数据结构实验之排序三:bucket sort

hint:
1大于等于100岁的按照100岁计算,自己一开始没有注意这个条件,数组越界然后提交后显示runtime error

数据结构实验之排序三:bucket sort
Time Limit: 150MS Memory Limit: 65536KB

Problem Description
根据人口普查结果,知道目前淄博市大约500万人口,你的任务是帮助人口普查办公室按年龄递增的顺序输出每个年龄有多少人,其中不满1周岁的按0岁计算,1到2周岁的按1岁计算,依次类推,大于等于100岁的老人全部按100岁计算。

Input
输入第一行给出一个正整数N(<=5000000),随后连续给出N个整数表示每个人的年龄,数字间以空格分隔。

Output
按年龄递增的顺序输出每个年龄的人口数,人口数为0的不输出,每个年龄占一行,数字间以一个空格分隔,行末不得有多余空格或空行。

Example Input
10
16 71 17 16 18 18 19 18 19 20

Example Output
16 2
17 1
18 3
19 2
20 1
71 1

Hint
Author
xam

以下为accepted代码

#include <stdio.h>
#include <string.h>
int main()
{
    int n, i, x;
    int book[104];
    scanf("%d", &n);
    memset(book, 0, sizeof(book));
    for(i = 0; i < n; i++)
    {
        scanf("%d", &x);
        if(x < 100)
            book[x] += 1;
        else
            book[100] += 1;
    }
    for(i = 0; i <= 100; i++)
    {
        if(book[i] != 0)
            printf("%d %d\n", i, book[i]);
    }
    return 0;
}


/***************************************************
User name: 
Result: Accepted
Take time: 104ms
Take Memory: 104KB
Submit time: 2017-02-22 15:38:31
****************************************************/

以下为runtime error代码——数组越界

#include <stdio.h>
#include <string.h>
int main()
{
    int n, i, x;
    int book[104];
    scanf("%d", &n);
    memset(book, 0, sizeof(book));
    for(i = 0; i < n; i++)
    {
        scanf("%d", &x);
        book[x] += 1;
    }
    for(i = 0; i <= 100; i++)
    {
        if(book[i] != 0)
            printf("%d %d\n", i, book[i]);
    }
    return 0;
}


/***************************************************
User name: 
Result: Runtime Error
Take time: 0ms
Take Memory: 0KB
Submit time: 2017-02-22 15:35:59
****************************************************/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值