求一个区间[a,b]中数字1出现的次数
2014-08-07 22:53
169 查看
原题见http://ac.jobdu.com/problem.php?pid=1373,如果【1,13】中出现1的整数有1,10,11,12,13 共6个
举个例子:
12105这个数字,千位上是2,那么千位上出现1的整数有多少个呢?1000~1999,11000~11999,总共是2x1000;(2 = 万位+1)
百位上是1,那么百位上出现1的整数有多少个呢?100~199,1100~1199...9100~9199,10100~10199,....11100~11199,12100~12105,总共是12x100+5+1;
十位上是0,那么十位上出现1的整数有多少个呢?10~19,110~119,210~219...1010~1019...10010~10019..12010~12019...总共:121x10
按照低位、当前位、高位来区分
求【1,N】中1出现的个数规律如下:
如果当前位小于1,那么个数等于高位*当前位率
如果当前位等于1,那么个数等于高位*当前位率 + 低位 + 1
如果当前位大于1,那么个数等于(高位+1)*当前位率
举个例子:
12105这个数字,千位上是2,那么千位上出现1的整数有多少个呢?1000~1999,11000~11999,总共是2x1000;(2 = 万位+1)
百位上是1,那么百位上出现1的整数有多少个呢?100~199,1100~1199...9100~9199,10100~10199,....11100~11199,12100~12105,总共是12x100+5+1;
十位上是0,那么十位上出现1的整数有多少个呢?10~19,110~119,210~219...1010~1019...10010~10019..12010~12019...总共:121x10
按照低位、当前位、高位来区分
求【1,N】中1出现的个数规律如下:
如果当前位小于1,那么个数等于高位*当前位率
如果当前位等于1,那么个数等于高位*当前位率 + 低位 + 1
如果当前位大于1,那么个数等于(高位+1)*当前位率
#include <iostream> using namespace std; #include <cstdio> #include <cstdlib> #include <cstring> #include <algorithm> #include <vector> #include <map> #include <queue> #include <stack> #include <set> int getnum(int n,int i){ int res = 0; int factor = 1; while(n/factor){ int high = (n/factor)/10; int curr = (n/factor)%10; int low = n - (n/factor)*factor; if(curr < i){ res = res + (high)*factor; }else if(curr == i){ res = res + (high)*factor + low + 1; }else if(curr > i){ res = res + (high+1)*factor; } factor = factor*10; } return res; } int main(){ int n; int a,b; //printf("%d",getnum(1,1)); while(scanf("%d%d",&a,&b) != EOF){ if(a > b) swap(a,b); if(a == 0) a = 1; printf("%d\n",getnum(b,1)-getnum(a-1,1)); } ///////////system("pause"); }
相关文章推荐
- 求一个区间[a,b]中数字1出现的次数
- 统计一个字符中中文,英文,数字出现的次数
- 统计一个数字在排序数组中出现的次数
- (使用STL自带的排序函数进行编程7.3.14)UVA 10062 Tell me the frequencies!(求一个字符出现的次数(包括字母和数字)&&按出现的次数降序)
- 找出一个数组中出现次数超过一半的那个数字,要求时间复杂度O(n)和空间复杂度为O(1)。
- 给定一个十进制正整数N,求出从1开始,到N的所有整数,数字1出现的次数(java实现)
- 数组中一个数字出现的次数超过了数组长度的一半,找出这个数字
- 【POJ2282 The Counting Problem】求区间[a,b]中每个数字出现的次数
- (4)统计一个数字,在排序数组中出现的次数
- 数组中有一个数字出现的次数超过了数组长度的一半,找出这个数字
- 统计一个数字在排序数组中出现的次数(二分法)
- 数组中有一个数字出现的次数超过了数组长度的一半,找出这个数
- 一个简单的算法---实现找出数组中一个数字出现次数最多的数字
- (Realx 数论1.28)POJ 2282 The Counting Problem(区间统计数字:统计a、b之间各个数字(0~9)出现的次数)
- 【算法习作】已知有一个数字在某数组中出现次数超过一半,求这个数
- 数组中有一个数字出现的次数超过了数组长度的一半,找出这个数字。
- 消除原理____假设数组中有一个数字出现的次数超过了数组长度的一半,试编程找出这个数字(qosrt 快速排序 哈希)
- 假设数组中有一个数字出现的次数超过了数组长度的一半,试编程找出这个数字(消除 qosrt 快速排序 哈希)
- 统计一个数字在排序数组中出现的次数(二分法)
- 找出一个数组中各个数字出现的次数