习题3-3 数数字 UVa1225

  • 出现了RE(runtime error)原因是maxn给小了
  • 没看清题目,是说1-n中数0-9的出现次数,并不是用户自己输入
  • memset(s(char类型数组),0,sizeof(s))..再用strcat能实现添加元素的作用。
  • 最后注意输出格式==>first
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>

#define maxn 100000
char s[maxn];
int count[10];
char tmp[5];
int main()
{
	
	int T;
	int len;
	int i;
	int n;
	scanf("%d",&T);
	while(T--)
	{
		scanf("%d",&n);
		memset(s,0,sizeof(s));
		memset(count,0,sizeof(int)*10);
		for(i=1;i<=n;i++)
		{
			sprintf(tmp,"%d",i);
			strcat(s,tmp);
		}
		len= strlen(s);
		for(i = 0;i<len;i++) count[s[i]-'0']++;
		int j;
		int first=1;
		for(j=0;j<10;j++) 
			if(first) 
				{printf("%d",count[j]);
				first=0;} 
			else  printf(" %d",count[j] );
		printf("\n");
	}
	return 0;
}
posted @ 2018-08-29 17:40  南邮果粒橙  阅读(282)  评论(0编辑  收藏  举报