[讨论]输入若干个0到9之间的整数,并统计其中各整数的个数.
大家一起做下,寻找最优算法.
----------------解决方案--------------------------------------------------------
#include <stdio.h>
#include <conio.h>
int main(void)
{
int saveIntNUM[10]={0,0,0,0,0,0,0,0,0,0};
int temp;
clrscr();
while(1)
{
scanf("%d",&temp);
if(temp>=0 && temp<=9)
saveIntNUM[temp]++;
else if(temp==-1)
break;
}
for(temp=0;temp<10;temp++)
printf("%d 的个数:%d\n",temp,saveIntNUM[temp]);
getch();
}
----------------解决方案--------------------------------------------------------
#include <stdio.h>
#include <conio.h>
int main(void)
{
int num,n=0;
scanf("%d",&num);
while(num)
{
num=num/10;
n++;
}
printf("%d",n);
getch();
return ;
}
----------------解决方案--------------------------------------------------------
#include"stdio.h"
#include"stdlib.h"
int main()
{
int a;
char b[10];
scanf("%d",&a);
itoa(a,b,10); //调用库函数了...
printf("%d",strlen(b));
getch();
return 0;
}
----------------解决方案--------------------------------------------------------