1.其实是dp题目。用建表方法避免了重复计算
1 #include <stdio.h>
2 const int maxn= 10008;
3 int count[maxn][10] = {0};
4 int main() {
5
6
7 int n;
8 char buf[maxn] = {0};
9
10 //计算每一位
11 for (int i = 1; i<= maxn; i++) {
12
13 sprintf(buf,"%d", i);
14 for (int j = 0; buf[j]; j++) {
15 count[i][buf[j]-'0']++;
16 }
17
18 }
19
20 //建立数据,防止重复计算
21 for (int i = 2; i <= maxn; i++) {
22
23 for (int j = 0; j < 10; j++) {
24
25 count[i][j] += count[i - 1][j];
26
27 }
28 }
29
30 scanf("%d", &n);
31 while (n > 0) {
32 int e;
33 scanf("%d", &e);
34
35 for (int i = 0; i < 10; i++) {
36 printf( i == 9? "%d\n" : "%d ", count[e][i]);
37 }
38
39 n--;
40 }
41 }
by sixleaves