想到两种方法,但有简单复杂之分,突然想到一句话,做题前一定要先理好思路,确定方法正确,千万不要等到写了很多行代码后才发现方法不对,那就太可惜了。
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main()
{
int i,j,k,r,s,t,m,n,yushu;
int num[10];
scanf("%d",&m);
for(k=1; k<=m; k++)
{
memset(num,0,sizeof(num));
scanf("%d",&n);
for(i=1; i<=n; i++)
{
s=i;
while(s>=1)
{
yushu=s%10;
s=s/10;
if(yushu == 0)
num[yushu]++;
else if(yushu == 1)
num[yushu]++;
else if(yushu == 2)
num[yushu]++;
else if(yushu == 3)
num[yushu]++;
else if(yushu == 4)
num[yushu]++;
else if(yushu == 5)
num[yushu]++;
else if(yushu == 6)
num[yushu]++;
else if(yushu == 7)
num[yushu]++;
else if(yushu == 8)
num[yushu]++;
else if(yushu == 9)
num[yushu]++;
}
}
t=0;
for(i=0; i<=9; i++)
{
if(t==0)
{
printf("%d",num[i]);
t++;
}
else
printf(" %d",num[i]);
}
printf("\n");
}
return 0;
}