两道桶排序题



问题一:描述:

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


自己的想法:这道题目难度不是很大,其实使用选择排序也是可以的。提醒自己要记得如何表达输入以EOF终止。

代码:

#include<stdio.h>
int main() {
    int a[21] = {0}, n,i = 0, j = 0;
    while (scanf("%d", &n) != EOF)
        a[n % 21]++;
    for (i = 0; i <= 20; i++) {
        while (a[i]--) {
            printf("%d ", i);
        }
    }
    return 0;
}

问题二:描述:给出一个英语句子,希望你把句子里的单词顺序都翻转过来

样例输入 Sample Input 

I love you

样例输出 Sample Output  

you love I

这道题目自己强化了gets()的使用输入字符串,同时再次使用了桶排序。

代码:

#include<stdio.h>
#include<string.h>
int main(){
int i,j=0,len,c[100]={0};
char a[10001]={0},b[10001]={0};
gets(a);
for(i=0;i<strlen(a);i++){
	if(a[i]==' ')j++;
	else c[j]++;
}
len=strlen(a);
while(j>=0){
	for(i=len-c[j];i<len;i++)printf("%c",a[i]);
	printf(" ");
	len=len-c[j]-1;
	j--;
}
return 0;
}
总结:希望自己今后在对比总结中去学习,同时记录下一些思维方法和一些打代码时细节。 
         


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值