内存:128  时间:10

题目描述

A Benedict monk No. 16 writes down the decimal representations of all natural numbers between and including

m and n , m n

. How many 0’s will he write down?

Input consists of a sequence of lines. Each line contains two unsigned 32-bit integers m and n, mn. The last line of input has the value of m negative and this line should not be processed.

For each line of input print one line of output with one integer number giving the number of 0’s written down by the monk.

 

输入

输出

样例输入

10 11
100 200
0 500
1234567890 2345678901
0 4294967295
-1 -1

样例输出

1
22
92
987654304
3825876150

提示

代码如下

#include <iostream>
using namespace std;
long long get0(long long n)
{
    long long zero=n/10+1;
    long long base=100;
    long long f,t;
    while(n/base)
    {
        f=n/base;
        t=n%base;
        if(t*10<base)  // 取0位为0
        {
            zero+=t+1;
            zero+=(f-1)*(base/10);
        }
        else
        {
            zero+=f*(base/10);
        }
        base*=10;
    }
    return zero;
}
int main()
{
    long long l,r;
    while(cin>>l>>r)
    {
        if(l<0)break;
        long long l0=get0(l);
        long long r0=get0(r);
        int divl= (l==0);
        while(l)
        {
            divl+= (l%10==0);
            l/=10;
        }
        cout<<r0-l0+divl<<endl;
    }
    return 0;
}

代码来源于互联网,仅供参考!