51nod-【1042 数字0-9的数量】

35 篇文章 0 订阅
基准时间限制:1 秒 空间限制:131072 KB 分值: 10  难度:2级算法题
 收藏
 关注
给出一段区间a-b,统计这个区间内0-9出现的次数。
比如 10-19,1出现11次(10,11,12,13,14,15,16,17,18,19,其中11包括2个1),其余数字各出现1次。
Input
两个数a,b(1 <= a <= b <= 10^18)
Output
输出共10行,分别是0-9出现的次数
Input示例
10 19
Output示例
1
11
1
1
1
1
1
1
1

1

如果是第一次接触这样的题目,推荐先看这个1009 数字1的数量 再次解释一下,随便举个数 456 求2出现的次数 我们在这里值分析百位的4  因为 4>2 把百位看成2,十位和个位有100种方案 然后百位可以取 0 1 2 3 这4个数,每个数对应有dp[2] 

#include<cstdio>
#include<cstring>
#include<cmath>
#define LL long long
LL dp[20]; 
void Init()
{
	memset(dp,0,sizeof(dp)); 
	int i;
	for(i=1;i<20;++i)
		dp[i]=dp[i-1]*10+pow(10,i-1);
}
LL solver(LL n,LL to)
{
	LL temp=1,tn=n,rail=0,result=0,digit=0;
	LL len=0;
	while(tn)
	{
		digit=tn%10;
		tn/=10;
		++len;
		if(to==digit)
		{
			result+=rail+1+digit*dp[len-1]; 
		} 
		else if(digit>to)
		{
			result+=digit*dp[len-1]+temp; 
		} 
		else if(digit<to) 
			result+=digit*dp[len-1]; 
		rail+=digit*temp;
		temp*=10;
	}
	if(to==0)
	{
		temp=1;
		while(n)
		{
			result-=temp;
			n/=10;
			temp*=10;
		} 
	}	 
	return result;
} 
int main()
{
	Init(); 
	LL a,b;
	while(scanf("%lld%lld",&a,&b)!=EOF)
	{
		LL i;
		for(i=0;i<=9;++i)
			printf("%lld\n",solver(b,i)-solver(a-1,i)); 
	} 
} 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值