Uva 11038 - How Many O's? 解题报告(计数)

来源:互联网 发布:盛世网络剧全集 编辑:程序博客网 时间:2024/04/29 07:26

Problem E: How many 0's?

A Benedict monk No. 16 writes down the decimal representations of all natural numbers between and including m and nm ≤ 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 nm ≤ n. 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.

Sample input

10 11100 2000 5001234567890 23456789010 4294967295-1 -1

Output for sample input

122929876543043825876150


    解题报告: 说实话,不喜欢这种题。一般思想不会太复杂,但是写起来还是很复杂的。代码如下:

#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>typedef long long LL;int digital[30];int digitalNum=0;LL ten[40];LL num[40];LL num2[40];void init(){    for(int i=0;i<40;i++)        ten[i] = pow(10, i)+0.5;    num[0]=1;    for(int i=1;i<40;i++)        num[i]=num[i-1]*10+ten[i];    num2[0]=1;    for(int i=1;i<40;i++)        num2[i]=num[i-1]*9;}void decomposition(LL n){    digitalNum=0;    while(n)        digital[digitalNum++]=n%10,n/=10;}LL solve(LL n){    if(n<0) return 0;    if(n<10) return 1;    decomposition(n);    LL ans=(digital[digitalNum-1]-1)*num[digitalNum-2];    for(int i=digitalNum-2;i>=1;i--)    {        if(digital[i]==0)        {            ans+=n%ten[i]+1;            continue;        }        ans+=ten[i];        ans+=digital[i]*num[i-1];    }    for(int i=0;i<digitalNum-1;i++)        ans+=num2[i];    ans++;    return ans;}int main(){    init();    LL m,n;    while(~scanf("%lld%lld", &m, &n) && (~m || ~n))        printf("%lld\n", solve(n)-solve(m-1));}


0 0
原创粉丝点击