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 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 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
- Uva 11038 - How Many O's? 解题报告(计数)
- UVA 11038 - How Many O's?(计数问题)
- UVA - 11038 How Many O's? (计数)
- uva 11038 How Many O's? 组合计数
- uva 11038 - How Many O's?(计数问题)
- UVa11038 How Many O's? (计数)
- UVa 11038 How Many 0's?(数学计数)
- Uva 11038 - How Many O's?
- uva 11038 How Many O's?
- UVA 11038 - How Many O's? (数学题)
- uva 11038 - How Many O's?
- UVa:11038 How Many O's?
- UVa-11038 How Many O's? (整数区间分解)
- UVA 11038 How Many O's(数位dp)
- UVA 11038 How Many O's? (数位统计 )
- UVa 11038 - How Many O's? (组合数学 数位统计)
- UVaOJ 11038 How Many O's?
- How Many Tables(解题报告)
- UNIX Network Programming笔记之第七章
- Java生产者与消费者(上)
- Linux Shell 常用快捷键
- poj2386 Lake Counting
- 【计算机网络常见面试题】EGP--IGP
- Uva 11038 - How Many O's? 解题报告(计数)
- Java的异常跟踪栈
- struts-config.xml配置
- 算法与排序--索引
- 编译原理之整体理解
- 人脸识别技术资料和源码(包含人脸库)
- Ubuntu 更改文件夹权限
- 分布式系统的事务处理
- 【二分图匹配】最大匹配-匈牙利算法BFS && DFS写法