poj 3286 How many 0's?

http://poj.org/problem?id=3286

求m和n之间有0的数的个数。 把题转换为从0到x中有0的数的个数,count1(n)-count1(m-1)就是从m到n的个数。

 1 #include <cstdio>
 2 #include <iostream>
 3 #include <cstring>
 4 #include <algorithm>
 5 #define ll long long
 6 using namespace std;
 7 
 8 ll f[100];
 9 ll m,n;
10 
11 void inti()
12 {
13     f[0]=1;
14     for(int i=1; i<15; i++)
15     {
16         f[i]=f[i-1]*10;
17     }
18 }
19 
20 ll count1(ll c)
21 {
22     if(c<0) return 0;
23     ll cnt=1;
24     for(int i=1; i<15; i++)
25     {
26         if(f[i]>c) break;
27         int m1=c/f[i];
28         int m2=c%f[i-1];
29         ll m3=(c%f[i]-c%f[i-1])/f[i-1];
30         if(m3==0) cnt+=(m1-1)*f[i-1]+m2+1;
31         else cnt+=m1*f[i-1];
32     }
33     return cnt;
34 }
35 
36 int main()
37 {
38     inti();
39     while(cin>>m>>n)
40     {
41         if(n==-1&&m==-1) break;
42         cout<<count1(n)-count1(m-1)<<endl;
43     }
44     return 0;
45 }
View Code

 

转载于:https://www.cnblogs.com/fanminghui/p/3776614.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值