poj 3286 How many 0's?

例子:123以内0的个数:

个位是0时:12*1;

十位是0时:1*10;

但是303的十位是0时的个数为2*10+4!

代码如下:

 

 1 #include<iostream>

 2 #include<stdio.h>

 3 #include<algorithm>

 4 #include<iomanip>

 5 #include<cmath>

 6 #include<cstring>

 7 #include<vector>

 8 #define ll __int64

 9 #define pi acos(-1.0)

10 #define MAX 50000

11 using namespace std;

12 ll solve(ll n)

13 {

14     if(n<0) return 0;

15     ll ans=1;

16     ll i,l,h,k;

17     for(i=1;i<=n;i*=10){

18         l=n%i;

19         h=n/i;

20         k=h%10;

21         h/=10;

22         if(k==0){

23             ans+=(h-1)*i+l+1;

24         }

25         else ans+=h*i;

26     }

27     return ans;

28 }

29 int main(){

30     ll n,m;

31     while(cin>>n>>m){

32         if(n<0&&m<0) break;

33         cout<<solve(m)-solve(n-1)<<endl;

34     }

35     return 0;

36 }
View Code

 

 

 

你可能感兴趣的:(poj)