题目:<a target=_blank href="http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=247&page=show_problem&problem=3666">UVa--1225</a>
思路:先将N内的所有数转化成字符储存在一个数组中,然后从头开始逐一查找它们各自出现的次数
<pre name="code" class="cpp">#include<stdio.h>
#include<string.h>
#define RE freopen("I:in.txt","r",stdin);
#define WE freopen("I:out.txt","w",stdout);
char str[50005]; //至少保证能存储10000以内的所有数字
int main(){
// RE;
// WE;
int T,N;
int i,j,temp;
scanf("%d",&T);<span style="white-space:pre"> </span>//测试个数T
while(T--){
scanf("%d",&N);<span style="white-space:pre"> </span>
j = 0;
for(i = 1;i <= N;i++){
temp = i;
while(temp > 0){
str[j++] = temp%10+'0';
temp /= 10;
} //将到N的所有数字都储存在字符串str中
}
int ans[10] = {0};
for(int k = 0;k < j;k++)
ans[str[k]-'0']++; //计算0~9各自出现的次数
for(int k = 0;k < 9;k++)
printf("%d ",ans[k]);
printf("%d\n",ans[9]); //分段输出,0~8一组,9为一组,控制换行
}
}