http://poj.org/problem?id=3286
题意:计算从[a,b]期间中总共有多少个0。
思路:比如1234,我们计算1到1234总共出现了多少个0,。 当个位有0的时候,出现了123次 当十位有0的时候,出现了12*10次
当百位有0的时候,出现了1*100次
但是若为1204,在计算十位0的个数的时候,是11*10+5,因为当前面是12的时候,最后一位只能取0-4,刚好5个
1 #include<stdio.h>
2 #include<string.h>
3 using namespace std;
4 typedef long long LL;
5 LL ab[20];
6
7 LL Solve(LL n)
8 {
9 LL sum=1;
10 if(n<0) return 0;
11 for(int i=1;i;i++)
12 {
13 if(ab[i]>n) break;
14 LL front=n/ab[i];
15 LL after=n%ab[i-1];
16 LL now=(n%ab[i]-n%ab[i-1])/ab[i-1];
17 if(now==0) sum+=(front-1)*ab[i-1]+after+1;
18 else sum+=front*ab[i-1];
19 }
20 return sum;
21 }
22
23 int main()
24 {
25 LL a,b;
26 ab[0]=1;
27 for(int i=1;i<15;i++) ab[i]=ab[i-1]*10;
28 while(scanf("%lld%lld",&a,&b)!=EOF)
29 {
30 if(a==-1 && b==-1) break;
31 LL ans1=Solve(a-1);
32 LL ans2=Solve(b);
33 printf("%lld\n",ans2-ans1);
34 }
35 return 0;
36 }