【UVA】1225 Digit Counting

题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=3666

题目:

Trung is bored with his mathematics homeworks. He takes a piece of chalk and starts writing a sequence of consecutive integers starting with 1 to N (1 < N < 10000). After that, he counts the number of times each digit (0 to 9) appears in the sequence. For example, with N = 13, the sequence is:

                                                                        12345678910111213

In this sequence, 0 appears once, 1 appears 6 times, 2 appears 2 times, 3 appears 3 times, and each digit from 4 to 9 appears once. After playing for a while, Trung gets bored again. He now wants to write a program to do this for him. Your task is to help him with writing this program.

Input

The input file consists of several data sets. The first line of the input file contains the number of data sets which is a positive integer and is not bigger than 20. The following lines describe the data sets.

For each test case, there is one single line containing the number N.

Output

For each test case, write sequentially in one line the number of digit 0, 1, . . . 9 separated by a space.

Sample Input

2

3

13

Sample Output

0 1 1 1 0 0 0 0 0 0

1 6 2 2 1 1 1 1 1 1

分析:

求1至n的序列中有多少个0-9。两种求法,直接穷举和打表。直接穷举,则每输入一个n,就从1至n计算0-9的个数。打表则,将1-10000的情况先计算一遍; 可以先将 每个数的0-9的计数看作是,前一个数0-9的计数 + 数本身的情况。(这样就不需要每次都从1至n 一 一计算)

另外,看了别人的代码,发现也可先将1至n的序列输出整合为字符串(可以用sprintf()函数)。然后从0到strlen(num)循环计数,从而对每一位的对应0-9进行计数。

代码:

直接穷举(未打表):

#include <stdio.h>
#include <string.h>
int main()
{
	int T,n;
	int a[10];
	int i,num;
	scanf("%d",&T);
	while (T--)
	{
		scanf("%d",&n);
		memset(a,0,sizeof(a));
		for (i=1; i<=n; i++)
		{
			num = i;
			while (num)
			{
				a[num%10]++;
				num /= 10;
			}
		}
		for (i=0; i<10; i++)
		{
			if (i) printf(" ");
			printf("%d",a[i]);
		}
		printf("\n");
	}
	return 0;
}

打表法: 

#include <stdio.h>
#include <string.h>
int main()
{
	int T,n,i,j;
	int num,a[10000][10];
	memset(a,0,sizeof(a));
	for (i=1;i < 10000; i++)
	{
		num = i;
		for (j=0; i>1 && j<10; j++)
			a[i][j]=a[i-1][j];
		while (num)
		{
			a[i][num%10]++;
			num /= 10;
		}
	}
	scanf("%d",&T);
	while (T--)
	{
		scanf("%d",&n);
		for (i=0; i<10; i++)
		{
			if (i) printf(" ");
			printf("%d",a[n][i]);
		}
		printf("\n");
	}
	return 0;
}

 

全部评论

相关推荐

02-01 12:05
复旦大学 Java
腾讯的提前批大概率应该是没有笔试的,但是这个时候有相当部分的同学简历估计都没有准备好,没准备好的同学也不用急,大部分都是3月之后开,这个时候开的绝大多数都是神仙打架,问的东西也比较难,打算投递的同学也多看下计算机网络和操作系统,腾讯对这部分的知识问的比较多。另外多刷下牛客的热门题库,刷题注意刷ACM模式,和牛客的周赛题,腾讯有的部门会从这里面出原题。我是@程序员花海关注我,带你了解更多校招资讯!
程序员花海:还没有来得及准备的同学可以看下学习路线:https://www.nowcoder.com/discuss/824693499982315520?sourceSSR=users算法题:https://www.nowcoder.com/feed/main/detail/20e7a999fa04485b88340a274411ca0d?sourceSSR=users八股文:https://www.nowcoder.com/discuss/833102362771251200?sourceSSR=users简历书写方式:https://www.nowcoder.com/discuss/839907820706205696?sourceSSR=users都是以前在牛客发的文章~
软开人,秋招你打算投哪些...
点赞 评论 收藏
分享
行云流水1971:这份实习简历的优化建议: 结构清晰化:拆分 “校园经历”“实习经历” 板块(当前内容混杂),按 “实习→校园→技能” 逻辑排版,求职意向明确为具体岗位(如 “市场 / 运营实习生”)。 经历具象化:现有描述偏流程,需补充 “动作 + 数据”,比如校园活动 “负责宣传” 可加 “运营公众号发布 5 篇推文,阅读量超 2000+,带动 300 + 人参与”;实习内容补充 “协助完成 XX 任务,效率提升 X%”。 岗位匹配度:锚定目标岗位能力,比如申请运营岗,突出 “内容编辑、活动执行” 相关动作;申请市场岗,强化 “资源对接、数据统计” 细节。 信息精简:删减冗余表述(如重复的 “负责”),用短句分点,比如 “策划校园招聘会:联系 10 + 企业,组织 200 + 学生参与,到场率达 85%”。 技能落地:将 “Office、PS” 绑定经历,比如 “用 Excel 整理活动数据,输出 3 份分析表;用 PS 设计 2 张活动海报”,避免技能单独罗列。 优化后需强化 “经历 - 能力 - 岗位需求” 的关联,让实习 / 校园经历的价值更直观。 若需要进一步优化服务,私信
实习,投递多份简历没人回...
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务