每日一题——第十四题

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

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

}
相关推荐
量子炒饭大师1 小时前
收集飞花令碎片——C语言字符函数与字符串函数
c语言·开发语言
web安全工具库3 小时前
Makefile 模式规则精讲:从 %.o: %.c 到静态模式规则的终极自动化
linux·运维·c语言·开发语言·数据库·自动化
earthzhang20216 小时前
【1028】字符菱形
c语言·开发语言·数据结构·c++·算法·青少年编程
承渊政道10 小时前
动态内存管理
c语言·c++·经验分享·c#·visual studio
Narcissiffo11 小时前
【C语言】str系列函数
c语言·开发语言
hqyjzsb12 小时前
2025年市场岗位能力重构与跨领域转型路径分析
c语言·人工智能·信息可视化·重构·媒体·改行学it·caie
小莞尔12 小时前
【51单片机】【protues仿真】基于51单片机智能窗帘系统
c语言·stm32·单片机·嵌入式硬件·物联网·51单片机
懒羊羊不懒@12 小时前
Java基础语法—最小单位、及注释
java·c语言·开发语言·数据结构·学习·算法
SundayBear13 小时前
嵌入式进阶:C语言内联汇编
c语言·开发语言·汇编
小龙报16 小时前
《算法通关指南---C++编程篇(2)》
c语言·开发语言·数据结构·c++·程序人生·算法·学习方法