每日一题——第十四题

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

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

}
相关推荐
祈安_3 天前
C语言内存函数
c语言·后端
norlan_jame4 天前
C-PHY与D-PHY差异
c语言·开发语言
czy87874754 天前
除了结构体之外,C语言中还有哪些其他方式可以模拟C++的面向对象编程特性
c语言
m0_531237174 天前
C语言-数组练习进阶
c语言·开发语言·算法
Z9fish5 天前
sse哈工大C语言编程练习23
c语言·数据结构·算法
代码无bug抓狂人5 天前
C语言之单词方阵——深搜(很好的深搜例题)
c语言·开发语言·算法·深度优先
CodeJourney_J5 天前
从“Hello World“ 开始 C++
c语言·c++·学习
枫叶丹45 天前
【Qt开发】Qt界面优化(七)-> Qt样式表(QSS) 样式属性
c语言·开发语言·c++·qt
with-the-flow5 天前
从数学底层的底层原理来讲 random 的函数是怎么实现的
c语言·python·算法
Sunsets_Red5 天前
P8277 [USACO22OPEN] Up Down Subsequence P 题解
c语言·c++·算法·c#·学习方法·洛谷·信息学竞赛