每日一题——第十四题

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

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;
            }
        }
    }

}
相关推荐
橘子真甜~30 分钟前
C/C++ Linux网络编程6 - poll解决客户端并发连接问题
服务器·c语言·开发语言·网络·c++·poll
小年糕是糕手2 小时前
【C++】C++入门 -- 输入&输出、缺省参数
c语言·开发语言·数据结构·c++·算法·leetcode·排序算法
Star在努力3 小时前
C语言复习八(2025.11.18)
c语言·算法·排序算法
赖small强3 小时前
【Linux C/C++开发】第26章:系统级综合项目理论
linux·c语言·c++
仟濹4 小时前
【C/C++】经典高精度算法 5道题 加减乘除「复习」
c语言·c++·算法
车端域控测试工程师5 小时前
Autosar网络管理测试用例 - TC003
c语言·开发语言·学习·汽车·测试用例·capl·canoe
EXtreme356 小时前
征服 C 语言文件 I/O:透视数据流、FILE* 核心机制与高效实践全指南
c语言··文件io
Bona Sun7 小时前
单片机手搓掌上游戏机(十二)—esp8266运行gameboy模拟器之编译上传
c语言·c++·单片机·游戏机
星期天29 小时前
3.2联合体和枚举enum,还有动态内存malloc,free,calloc,realloc
c语言·开发语言·算法·联合体·动态内存·初学者入门·枚举enum
自信150413057599 小时前
初学者小白复盘23之——联合与枚举
c语言·1024程序员节