当前位置: 代码迷 >> 综合 >> 又是一道题 Digit Counting
  详细解决方案

又是一道题 Digit Counting

热度:26   发布时间:2023-11-25 14:36:57.0

比较简单吧!

Trung is bored with his mathematics homeworks. He takes a piece of chalk and starts writing a sequenceof consecutive integers starting with 1 to N (1 < N < 10000). After that, he counts the number oftimes each digit (0 to 9) appears in the sequence. For example, with N = 13, the sequence is:

12345678910111213

In this sequence, 0 appears once, 1 appears 6 times, 2 appears 2 times, 3 appears 3 times, and eachdigit from 4 to 9 appears once. After playing for a while, Trung gets bored again. He now wants towrite a program to do this for him. Your task is to help him with writing this program.

Input

The input file consists of several data sets. The first line of the input file contains the number of datasets which is a positive integer and is not bigger than 20. The following lines describe the data sets.For each test case, there is one single line containing the number N.

Output

For each test case, write sequentially in one line the number of digit 0, 1, . . . 9 separated by a space.

Sample Input

23

13

Sample Output 

01 1 1 0 0 0 0 0 0

1 6 2 2 1 1 1 1 1 1

代码:

#include<stdio.h>
void f(int);
int s[11];
int main()
{int T,n,i,a,q,k; 	scanf("%d",&T);while(T--){	for(i=0;i<11;i++)s[i]=0;scanf("%d",&n);for(a=1;a<=n;a++){q=a;while(q!=0){k=q%10;f(k);q/=10;}		}for(i=0;i<9;i++)printf("%d ",s[i]);printf("%d\n",s[i]);	}return 0;
}
void f(int q)
{if(q==0)	s[0]++;if(q==1)	s[1]++;if(q==2)	s[2]++;	if(q==3)	s[3]++;if(q==4)	s[4]++;if(q==5)	s[5]++;if(q==6)	s[6]++;if(q==7)	s[7]++;if(q==8)	s[8]++;if(q==9)	s[9]++;	
}