计数问题
2011-04-17 21:30
225 查看
计数问题
TimeLimit:
1 Second MemoryLimit: 32 Megabyte
Description
给你两个数a和b,你的任务是计算出1在a和b之间出现的次数,比如说,如果a=1024,b=1032,那么a和b之间的数就是:
1024 1025 1026 1027 1028 1029 1030 1031
1032
则有10个1出现在这些数中。
Input
输入不会超过500行。每一行有两个数a和b,a和b的范围是0 < a, b < 100000000。输入两个0时程序结束,两个0不作为输入样例。
Output
对于每一对输入的a和b,输出一个数,代表1出现的个数。
Sample Input
1 10
44 497
346 542
1199 1748
1496 1403
1004 503
1714 190
1317 854
1976 494
1001 1960
0 0
Sample Output
2
185
40
666
113
105
1133
512
1375
1256
TimeLimit:
1 Second MemoryLimit: 32 Megabyte
Description
给你两个数a和b,你的任务是计算出1在a和b之间出现的次数,比如说,如果a=1024,b=1032,那么a和b之间的数就是:
1024 1025 1026 1027 1028 1029 1030 1031
1032
则有10个1出现在这些数中。
Input
输入不会超过500行。每一行有两个数a和b,a和b的范围是0 < a, b < 100000000。输入两个0时程序结束,两个0不作为输入样例。
Output
对于每一对输入的a和b,输出一个数,代表1出现的个数。
Sample Input
1 10
44 497
346 542
1199 1748
1496 1403
1004 503
1714 190
1317 854
1976 494
1001 1960
0 0
Sample Output
2
185
40
666
113
105
1133
512
1375
1256
#include"RunTime.h" #include <iostream> using namespace std; int countOne(int n) { int f = 1; int count = 0; int l = 0; int h = 0; int c = 0; while (n/f) { l = n %f; c = (n/f)%10; h = n/(f*10); count += h * f; switch(c) { case 0://通过分析可知,当当前位为0时,1的个数只受高位的影响 break; case 1://为1时,受高位和低位的影响 count += l + 1; break; default://大于1的时候为高位的数+1 count += f; } f*=10; } return count; } int main() { RunTime rt; int m,n; while (1) { cin>>m>>n; if(m==0&&n==0)break; rt.Begin(); if(m>n) cout<<countOne(m)-countOne(n-1)<<endl; else cout<<countOne(n)-countOne(m-1)<<endl; rt.End(); rt.ShowTime(); } return 0; }
相关文章推荐
- NOIP 2013 普及组 复赛 count 计数问题
- 数论基础之组合数&计数问题
- 数字计数问题(算法实验)
- HDU 4095 Y (树上计数问题)
- 计蒜客-计数问题
- UVA 11529 - Strange Tax Calculation(计数问题)
- [SQL Server]关于标识列从1开始计数的问题
- 有限背包计数问题
- 重装SQL Server 2005遇到问题“性能监视器计数要求(错误)”的解决方法
- 序关系计数问题
- 计数问题
- 计数问题
- 南京邮电oj 1222序关系计数问题
- [背包DP || 多项式] 51Nod 1597 有限背包计数问题
- UVA-11806 Cheerleaders 计数问题 容斥定理
- 解决 PHPEXCEL 纯数字 科学计数的问题
- 动态规划第4讲——计数问题中的DP算法
- Game of Connections -组合数学中的计数问题
- hpu 问题 H: KACA的杨辉计数
- 挑战程序设计竞赛2.3.3 有关计数问题的DP 多重集组合数