问题一:描述:
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;
}
总结:希望自己今后在对比总结中去学习,同时记录下一些思维方法和一些打代码时细节。