您的位置:首页 > 其它

poj 3286 How Many 0's?

2016-09-25 10:40 337 查看
Description

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

Input consists of a sequence of lines. Each line contains two unsigned 32-bit integers m and n, m ≤ n. 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

Source

Waterloo Local Contest, 2006.5.27

/*
Problem: 3286       User: saruka
Memory: 380K        Time: 79MS
Language: G++       Result: Accepted
*/
#include<cstdio>
inline int getint()
{
int r = 0, k = 1;
char c = getchar();
for(; c < '0' || c > '9'; c = getchar())
if(c == '-') k = -1;
for(; c >= '0' && c <= '9'; c = getchar())
r = r * 10 + c - '0';
return r * k;
}
long long n, m;
long long does(long long num)
{
if(num < 0) return 0;
long long ans = 1, div = 10;
while(num / div)
{
if((num % div - num % (div / 10)) == 0)
{
ans += ((num / div) - 1) * (div / 10) + (num % (div / 10)) + 1;
}
else
{
ans += (num / div) * (div / 10);
}
div *= 10;
}
return ans;
}
int main()
{
while((scanf("%lld%lld", &m, &n)== 2) && (m != -1) && (n != -1))
{
long long result_1 = does(n);
long long result_2 = does(m - 1);
printf("%lld\n", result_1 - result_2);
}
return 0;
}


Powered By Saruka

Copyright © 2016 All Rights Reserved.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  poj