Bucket sort 桶排序(含EOF)

Description:
Input a couple of numbers (0~100) and end of EOF.

If number > 20, it shuold be mapped to [0, 20],whitch is mean ‘number %= 21’

Output all these numbers by ascending oder.

Output format: behind every number, there is a space ’ ‘. And no ‘\n’

For example:

[Input]

1 2 22 3 3 44 5 55 6 66 7 20 21 23 100 101

[Output]

0 1 1 2 2 2 3 3 3 5 6 7 13 16 17 20

Hint:
输入数据范围较集中,建议用桶排序。

windows下出入EOF:换到新的一行,输入ctrl+Z

linux下输入EOF:换到新的一行,输入ctrl+D
代码块
使用冒泡排序

#include<stdio.h>
int main() {
    int a[100], b, t;
    int i = 0, k, j;
    while ((scanf("%d", &b))!= EOF) {
        a[i] = b;
        if (a[i] > 20) {
            a[i] = a[i] % 21;
        }
        i++;
    }
    // 注意冒泡排序写法
    for (k = 0; k < i; k++)
        for (j = 0; j < i - 1; j++)
        // 注意此处应该写i—1
        if (a[j] > a[j + 1]) {
            t = a[j];
            a[j] = a[j + 1];
            a[j + 1] = t;
        }
    for (k = 0; k < i; k++)
    printf("%d ", a[k]);
return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值