如题所述
#include <stdio.h>
int main(int argc, char* argv[])
{
char ch;
int counter[10];
for (int i=0;i<10;i++)
{
counter[i]=0;
}
while((ch = getchar()) != '!')
{
switch (ch)
{
case '0':
counter[0]++;
break;
case '1':
counter[1]++;
break;
case '2':
counter[2]++;
break;
case '3':
counter[3]++;
break;
case '4':
counter[4]++;
break;
case '5':
counter[5]++;
break;
case '6':
counter[6]++;
break;
case '7':
counter[7]++;
break;
case '8':
counter[8]++;
break;
case '9':
counter[9]++;
break;
default:
break;
}
}
for (i=0;i<10;i++)
{
//cout<<"数字"<<i<<"的个数是:"<<counter[i]<<endl;//如果用C++;
printf("数字");
printf("%d",i);
printf("的个数是:%d\n",counter[i]);
}
return 0;
}
注释:
VC6.0测试通过;
int main(int argc, char* argv[])
{
char ch;
int counter[10];
for (int i=0;i<10;i++)
{
counter[i]=0;
}
while((ch = getchar()) != '!')
{
switch (ch)
{
case '0':
counter[0]++;
break;
case '1':
counter[1]++;
break;
case '2':
counter[2]++;
break;
case '3':
counter[3]++;
break;
case '4':
counter[4]++;
break;
case '5':
counter[5]++;
break;
case '6':
counter[6]++;
break;
case '7':
counter[7]++;
break;
case '8':
counter[8]++;
break;
case '9':
counter[9]++;
break;
default:
break;
}
}
for (i=0;i<10;i++)
{
//cout<<"数字"<<i<<"的个数是:"<<counter[i]<<endl;//如果用C++;
printf("数字");
printf("%d",i);
printf("的个数是:%d\n",counter[i]);
}
return 0;
}
注释:
VC6.0测试通过;
温馨提示:答案为网友推荐,仅供参考
第1个回答 推荐于2018-03-20
include <stdio.h>
main()
{int a[10]={0},n;
char ch;
while((ch=getchar())!='!')
{n=ch-'0';
a[n]=++a[n];}
for(n=0;n<10;n++)
printf("%d的个数为%d\n",n,a[n]);
}本回答被提问者和网友采纳
main()
{int a[10]={0},n;
char ch;
while((ch=getchar())!='!')
{n=ch-'0';
a[n]=++a[n];}
for(n=0;n<10;n++)
printf("%d的个数为%d\n",n,a[n]);
}本回答被提问者和网友采纳
第2个回答 2012-12-16
#include <stdio.h>#include <stdlib.h>
int main()
{
char a;
int i,count1=0;
for (i=0; ;i++)
{
scanf ("%c",&a);
if (a=='!')
break ;
else if (a=='1')
count1++;
else if (以下省略)
}
printf ("%d %d....",count1,count2......);
return 0;
}
int main()
{
char a;
int i,count1=0;
for (i=0; ;i++)
{
scanf ("%c",&a);
if (a=='!')
break ;
else if (a=='1')
count1++;
else if (以下省略)
}
printf ("%d %d....",count1,count2......);
return 0;
}