UVA - 11038 How Many O's? (计数)
2014-08-20 18:27
351 查看
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 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.
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.
[/code]
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 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, m ≤ 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 11 100 200 0 500 1234567890 2345678901 0 4294967295 -1 -1
Output for sample input
1 22 92 987654304 3825876150 题意: 求[n, m]之间包含0的数字的个数 思路:先转化为前缀问题,就是cal(m)-cal(n-1),枚举每一位,如果第i位是0的话,那么为了不让数大于原本的数的话,那么它的左边取[1, a-1]的时候,右边可以取任意值 如果取a的话,那么只能取0到右边的最大;如果是非0的话,那么让这位为0的话,就可以左边随便取,右边随便取[code]#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> typedef long long ll; using namespace std; ll n, m; ll cal(ll x) { if (x < 0) return 0; ll ans = 1, cnt = 1, tmp = 0; while (x >= 10) { ll cur = x % 10; x /= 10; if (cur) ans += x * cnt; else ans += (x-1) * cnt + tmp + 1; tmp += cur * cnt; cnt *= 10; } return ans; } int main() { while (scanf("%lld%lld", &n, &m) != EOF && n != -1 && m != -1) { printf("%lld\n", cal(m) - cal(n-1)); } return 0; }
[/code]
相关文章推荐
- uva 11038 How Many O's? 组合计数
- UVA11038- How Many O's?(组合数学)
- UVA11038- How Many O's?(组合数学)
- UVa 11038 - How Many O's? (组合数学 数位统计)
- uva 11038 - How Many O's?
- UVa:11038 How Many O's?
- UVA 10061 How many zero's and how many digits ?(数论)
- UVa 10061 - How many zero's and how many digits ?
- Uva 11038 - How Many O's? 解题报告(计数)
- UVA 11038 - How Many O's?(计数问题)
- uva 10061: How many zero's and how many digits?
- UVa11038 How Many O's? (计数)
- uva 10061 How many zero's and how many digits ?(在不同进制下分解因子)
- uva 11038 - How Many O's?(计数问题)
- UVa 10061 How many zero's and how many digits ? (任意进制下的阶乘长度和尾0的数目)
- UVA - 10061 How many zero's and how many digits ?
- uva 10061 - How many zero's and how many digits ?
- uva 10061 How many zero's and how many digits ?(不同进制阶乘末尾几个0)+poj 1401
- How to troubleshoot 'Procedure or function has too many arguments specified' in asp.net 2.0
- UVA 11038 How Many O's? (数位统计 )