输入一串字符,以“ !”结束,分别统计其中每个数字字符0,1,2,….9出现的次数。用c语言写
3个回答
展开全部
#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测试通过;
展开全部
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]);
}
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
#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;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询