对数字、空格和其他字符进行计数。此处的空白包括“\n”、“和”\t”。注意,在输入样本的末尾有一个不可见的“\n”。
count digits, white space, and other characters. White space here includes '\n', ' ' and '\t'. Noting that there is an unvisible '\n' at the end of input sample.
输入格式:
text
输出格式:
digits = x x x x x x x x x x, white space = x, other = x
输入样例:
12 a 3
结尾无空行
输出样例:
digits = 0 1 1 1 0 0 0 0 0 0, white space = 3, other = 1
结尾无空行
常见的一道题,细节比较多,比如输出的格式,很容易少空格之类的。
#include<stdio.h>
int main(void){
char c=0;
int a[10]={0};
int i,w=0,o=0;
while((c=getchar())!=EOF){
if(c>='0'&&c<='9'){
a[c-'0']++;//这里要注意c-'0',直接打c是不行的
}
else if(c==' '||c=='\n'||c=='\t')
w++;
else o++;
}
printf("digits = ");
for(i=0;i<=9;i++){
if(i==9)
printf("%d",a[i]);
else printf("%d ",a[i]);
}
printf(", white space = %d",w);
printf(", other = %d",o);
return 0;
}