题目链接:http://vjudge.net/problem/UVA-1225
#include <iostream> #include <cstring> #include <cstdio> using namespace std; int f[10000][10]; int main() { memset(f, 0, sizeof(f)); for (int i = 1 ; i < 10000 ; ++ i) { for (int j = 0 ; j < 10 ; ++ j) f[i][j] = f[i-1][j]; int left = i; while (left) { f[i][left%10] ++; left /= 10; } } int t,n; while (~scanf("%d",&t)) while (t --) { scanf("%d",&n); for (int i = 0 ; i < 9 ; ++ i) printf("%d ",f[n][i]); printf("%d ",f[n][9]); } return 0; }
还写了一个类似的代码,不过WA
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 using namespace std; 5 6 int main() 7 { 8 int t,n,i,temp; 9 char ans[10]; 10 scanf("%d",&t); 11 while(t--) 12 { 13 memset(ans,0,sizeof(ans)); 14 scanf("%d",&n); 15 for(i=1;i<=n;i++) 16 { 17 temp=i; 18 while(temp) 19 { 20 ans[temp%10]++; 21 temp/=10; 22 } 23 } 24 for(i=0;i<9;i++) 25 printf("%d ",ans[i]); 26 printf("%d ",ans[9]); 27 } 28 return 0; 29 }