每日一题——第十四题

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

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

}
相关推荐
Eternity∞1 小时前
基于Linux系统vim编译器情况下的C语言学习
linux·c语言·开发语言·学习·vim
HUST2 小时前
C语言第十一讲: 深入理解指针(1)
c语言·开发语言
SoveTingღ3 小时前
【C语言】什么是野指针?
c语言·指针·嵌入式软件
lowhot3 小时前
C语言UI框架
c语言·开发语言·笔记·ui
ベadvance courageouslyミ4 小时前
项目一(线程邮箱)
c语言·线程·makefile·进程间通信·线程邮箱
Herbert_hwt4 小时前
C语言表达式求值详解:从原理到实战的完整指南
c语言
朔北之忘 Clancy4 小时前
2025 年 6 月青少年软编等考 C 语言一级真题解析
c语言·开发语言·c++·学习·算法·青少年编程·题解
梁山1号5 小时前
【关于CAN】
c语言·stm32·单片机
VekiSon5 小时前
综合项目实战——电子商城信息查询系统
linux·c语言·网络·http·html·tcp·sqlite3
客卿1235 小时前
C语言刷题--合并有序数组
java·c语言·算法