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