|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
记入输入的每个数(0到9)的次数。
代码如下,没有error 没有warming,但无法得出正确结果
# include<stdio.h>
int main()
{
const int number = 10;
int x;
int count[number];
int i;
for(i=0; i<number; i++)
{
count[i]=0;
}
scanf("%d", &x);
while(x!=1)
{
if( x>=0 && x<=9 )
{
count[x]++;
}
scanf("%d", &x);
}
for(i=0; i<number; i++)
{
printf("%d:%d\n", i, count[i]);
}
return 0;
}
程序没问题啊,只是无法记录1而已,因为一旦输入1,程序就退出循环了,
而且输入的若是超过一位数,是不会被记录的,没输入一位数就需要回车一下
若想连续输入,应该用%c而不应该用%d。
- # include<stdio.h>
- int main()
- {
- const int number = 10;
- char x;
- int count[number];
- int i;
- for(i=0; i<number; i++)
- {
- count[i]=0;
- }
- scanf("%c", &x);
- while(x!='\n')
- {
- if( x>='0' && x<='9' )
- {
- count[x-'0']++;
- }
- scanf("%c", &x);
- }
- for(i=0; i<number; i++)
- {
- printf("%d:%d\n", i, count[i]);
- }
- return 0;
- }
复制代码
运行:
- 输入内容:1112233344445666778900
- 输出结果:
- 0:2
- 1:3
- 2:2
- 3:3
- 4:4
- 5:1
- 6:3
- 7:2
- 8:1
- 9:1
复制代码
|
|