3286 How many 0's? 计算(n,m)中有多少个0

How many 0's?
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 1744 Accepted: 816

Description

A Benedict monk No.16 writes down the decimal representations of all natural numbers between and including m and n, mn. How many 0's will he write down?

Input

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.

Output

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 11
100 200
0 500
1234567890 2345678901
0 4294967295
-1 -1

Sample Output

1
22
92
987654304
3825876150
#include "iostream"
#include "cstring"
#include "cstdio"
#include "cmath"
using namespace std;
__int64 n,m;
struct node
{
    __int64 num[12];
}cnt,res;
void prntf(node nn)
{
    printf("%I64d/n",nn.num[0]);
}
node countnum(__int64 t)//过程(1-t) 中(0-9)的总个数
{
    if(t==-1) {node tt;memset(tt.num,0,sizeof(tt.num));tt.num[0]=-1;return tt;}
    __int64 times=1,temp,td;
    __int64 i,j,start;
    node tmp;
    memset(tmp.num,0,sizeof(tmp.num));
    while (t>=times)
    {
        td=t/(times*10);
        for (i=0;i<=9;i++)
            tmp.num[i]+=td*times;
        if (td) tmp.num[0]-=times;
        temp=(t/times)%10;//当前尾数
        if (t>=times*10)//若只剩一位数的时候0是不要算的 因为我们是从1开始的
            j=0;
        else j=1;
        for (i=j;i<temp;i++)
            tmp.num[i]+=times;
        tmp.num[temp]+=t%times+1;
        times*=10;
    }
    return tmp;
}
void solve()
{
    __int64 i,j;
    node a,b;
    a=countnum(n-1);a.num[0]++;
    b=countnum(m);b.num[0]++;
    node c;
    for (i=0;i<10;i++)
        c.num[i]=b.num[i]-a.num[i];
    prntf(c);
}
int main()
{
    //freopen("R:/in.txt","r",stdin);
    while (scanf("%I64d%I64d",&n,&m)!=EOF)
    {
        if (n==-1&&m==-1)  break;
        solve();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值