每日一题——第十四题

题目:输入一行数字,将其按升序输出,且奇数在前,偶数在后

c 复制代码
#include<stdio.h>

void bubbleSort(int* arr, int n);
int main(){
    int n, i, input_nums;
    printf("请输入数字个数: ");
    scanf("%d", &n);

    int odd[n / 2 + 1], even[n / 2 + 1];//用来存放奇数和偶数
    int odd_count = 0, even_count = 0;

    for(i = 0; i < n; i ++){

        scanf("%d", &input_nums);

        //把数据存入相应的数组中
        if(input_nums % 2 == 0){

            even[even_count++] = input_nums;
        }
        else{
            odd[odd_count++] = input_nums;
        }
    }

    //对数据元素进行排序
    bubbleSort(odd, odd_count);
    bubbleSort(even, even_count);

    //输出排序后的奇数
    printf("升序输出,奇数在前:");
    for(i = 0; i < odd_count; i++){
        printf("%d", odd[i]);
    }

    //输出排序后的偶数
    for(i = 0; i < even_count; i++){
        printf("%d", even[i]);
    }

    printf("\n");

    return 0;
}

//冒泡排序
void bubbleSort(int* arr, int n){
    int i, j, temp;

    for(i = 0; i < n - 1; i ++){
        for(j = 0; j < n - (i + 1); j ++){

            //升序排列
            if(arr[j] > arr[j + 1]){

                temp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = temp;
            }
        }
    }

}
相关推荐
多弗朗皮卡丘13 小时前
C语言梦开始的地方3
c语言·开发语言
天空'之城1 天前
C 语言工业级通用组件手写 24:互补滤波
c语言·开发语言·互补滤波·嵌入式算法·工业级组件
stevenseahang1 天前
C语言标准演化史:从K&R到GNU,谁才是正统?
c语言·gnu·可移植性·标准演化·ansic
别动我齐刘海1 天前
Day6 unitree_G1人形机器人GMR—— MotionInput
c语言·c++·人工智能·学习·机器学习·机器人·github
AI科技星1 天前
曲率‑挠率与 $\boldsymbol{\omega/c}$ 的关系、精算验证及其物理意义
c语言·开发语言·线性代数·算法·决策树·机器学习·ai科技星
坏柠1 天前
C语言进阶:从零实现一个可靠的环形缓冲区,吃透回绕、边界与并发
java·c语言·算法
choumin2 天前
C程序能调用extern “C“声明的带默认参数的C++函数吗?
c语言·c++·extern c·默认参数
LONGZETECH2 天前
传统实训 vs 仿真实训:从技术架构拆解新能源汽车教学的范式之变
c语言·3d·unity·架构·汽车
白白白小纯2 天前
每日算法day3—回文链表,链表分割
c语言·数据结构·算法·leetcode
Aurorar0rua2 天前
CS50 x 2024 Notes Algorithms - 08
c语言·开发语言·算法·学习方法