谢谢,怎么用c语言写一个程序:用户输入数字,然后统计每个数字出现

谢谢,怎么用c语言写一个程序:用户输入数字,然后统计每个数字出现的次数。

第1个回答  2016-11-04
#include <stdio.h>

int main()
{
int i,num;
scanf("%d",&num);
int array[10]={0};
while(num!=0)
{
for(i=0;i<10;i++)
{
if(num%10==i) array[i]++;
}
num/=10;
}
for(i=0;i<10;i++)
{
if(array[i]!=0)
printf("%d:%d\n",i,array[i]);
}
}本回答被提问者采纳
第2个回答  2016-11-04
//#include "stdafx.h"//vc++6.0加上这一行.
#include "stdio.h"
int main(void){
char ch;
int Dec[10]={0},letter=0,other=0,i=0;
while(1){
if((ch=getchar())>='0' && ch<='9')
Dec[ch-'0']++;
else if(ch>='A' && ch<='Z' || ch>='a' && ch<='z')
letter++;
else if(ch=='\n')
break;
else other++;
}
for(i=0;i<10;printf("Number%d: %d\n",i,Dec[i++]));
printf("Characters: %d\n",letter);
printf("Other: %d\n",other+1);
return 0;
}

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网