Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 3580 | Accepted: 1944 |
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 11100 2000 5001234567890 23456789010 4294967295-1 -1
Sample Output
122929876543043825876150
Source
, 2006.5.27
问题链接:。
问题简述:输入无符号整数m和n,满足m<=n,计算m到n(包括m和n)之间各个数中包含多少个0。
问题分析:先分别计算0到m-1和0到n之间数的0个数,结果=0到n之间数的0个数-0到m-1之间数的0个数。计算0到n之间数的0个数时,先考虑1位数、2位数、......,在小于n的区间逐步统计。程序说明:数组radix[]计算存放10进制的位权备用。函数countzero()用于统计0到n之间数的0个数。
参考链接:。
AC的C语言程序如下:
/* POJ3286 How many 0's? */#includetypedef long long LL;#define MAXN 10LL radix[MAXN+1];void maketable(){ int i; radix[0] = 1; for(i=1; i<=MAXN; i++) radix[i] = radix[i-1] * 10;}LL countzero(LL n){ if(n < 0) return 0; LL sum = 1; int i = 1; while(radix[i] <= n) { LL digit, left, right; digit = n % radix[i] / radix[i - 1]; if (digit == 0) { left = n / radix[i]; right = n % radix[i - 1]; sum += (left - 1) * radix[i - 1] + right + 1; } else { left = n / radix[i]; sum += left * radix[i - 1]; } i++; } return sum;}int main(void){ maketable(); LL m, n; while(scanf("%lld%lld", &m, &n) != EOF) { if(m == -1 && n == -1) break; printf("%lld\n", countzero(n) - countzero(m-1)); } return 0;}