蠢办法。。输出注意不要多输了空格。
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<string.h> int b[10], a[10005]; char s[40000]; int main(){ int m , n; scanf("%d",&m); while (m--){ scanf("%d", &n); a[0] = 1; for (int i = 1; i < n; i++){ a[i] = a[i - 1] + 1; } for (int j = 0,p=0; a[p];) { if (a[p] < 10){ s[j] = a[p] + '0'; j++; p++; } else if (a[p] < 100){ s[j] = a[p] / 10 + '0'; s[j + 1] = a[p] % 10 + '0'; j += 2; p++; } else if (a[p] < 1000){ s[j] = a[p] / 100 + '0'; s[j + 1] = a[p] % 100 / 10+'0'; s[j + 2] = a[p] % 100 % 10+'0'; j += 3; p++; } else { s[j] = a[p] / 1000 + '0'; s[j + 1] = a[p] % 1000 / 100 + '0'; s[j + 2] = a[p] % 100 / 10+'0'; s[j + 3] = a[p] % 10+'0'; j += 4; p++; } } for (int j = 0; j < strlen(s); j++){ for (int k = 0; k < 10; k++){ if (s[j] - '0' == k) b[k]++; } } for (int l = 0; l < 10; l++){ if (l==0) printf("%d", b[l]); else printf(" %d",b[l]); } printf("\n"); memset(b, 0, sizeof(b)); memset(a, 0, sizeof(a)); memset(s, '\0', sizeof(s)); } return 0; }