先打出0~8位数,分别可以被整十/百/千/万...整除时 , 各个数字出现了几次的表
先把每要查询的数字的每一位在表里查询得到一个结果
但是这样是不全面的,考虑这样的情况: 例如2345这样的数 234* 这种情况下 4出现了5次 23**这种情况下3出现了45次 2***中2出现了345次等.....从后往前扫一遍即可
其中0的情况比较特殊,简单的扫一遍会漏掉很多可能 比如 5050时: 500*的情况下,第2个0就没有考虑到,所以还要进行一次补0的操作.
排除首位从前往后扫,遇到每一个不为0的数就考虑将这位变成0,如果这个数后面还有1个数就有9中可能,后面有2个数就有108种可能(9+99) 有3个数就有(9+99+999)种可能...依次类推.....
结果加到一起就是答案
The Counting Problem
Time Limit: 10000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 421 Accepted Submission(s): 169
Problem Description
Given two integers a and b, we write the numbers between a and b, inclusive, in a list. Your task is to calculate the number of occurrences of each digit. For example, if a = 1024 and b = 1032, the list will be
1024 1025 1026 1027 1028 1029 1030 1031 1032
there are ten 0‘s in the list, ten 1‘s, seven 2‘s, three 3‘s, and etc.
Input
The input consists of up to 500 lines. Each line contains two numbers a and b where 0 < a, b < 100000000. The input is terminated by a line `0 0‘, which is not considered as part of the input.
Output
For each pair of input, output a line containing ten numbers separated by single spaces. The first number is the number of occurrences of the digit 0, the second is the number of occurrences of the digit 1, etc.
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
1 2 1 1 1 1 1 1 1 1 85 185 185 185 190 96 96 96 95 93 40 40 40 93 136 82 40 40 40 40 115 666 215 215 214 205 205 154 105 106 16 113 19 20 114 20 20 19 19 16 107 105 100 101 101 197 200 200 200 200 413 1133 503 503 503 502 502 417 402 412 196 512 186 104 87 93 97 97 142 196 398 1375 398 398 405 499 499 495 488 471 294 1256 296 296 296 296 287 286 286 247
Source
/* *********************************************** Author :CKboss Created Time :2015年02月01日 星期日 13时58分50秒 File Name :HDOJ1663_3.cpp ************************************************ */ #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <string> #include <cmath> #include <cstdlib> #include <vector> #include <queue> #include <set> #include <map> using namespace std; /// 不补0的表 int no[][10] = { 1,0,0,0,0,0,0,0,0,0, 1,1,0,0,0,0,0,0,0,0, 1,1,1,0,0,0,0,0,0,0, 1,1,1,1,0,0,0,0,0,0, 1,1,1,1,1,0,0,0,0,0, 1,1,1,1,1,1,0,0,0,0, 1,1,1,1,1,1,1,0,0,0, 1,1,1,1,1,1,1,1,0,0, 1,1,1,1,1,1,1,1,1,0, 1,1,1,1,1,1,1,1,1,1, 0,0,0,0,0,0,0,0,0,0, 1,2,1,1,1,1,1,1,1,1, 2,12,3,2,2,2,2,2,2,2, 3,13,13,4,3,3,3,3,3,3, 4,14,14,14,5,4,4,4,4,4, 5,15,15,15,15,6,5,5,5,5, 6,16,16,16,16,16,7,6,6,6, 7,17,17,17,17,17,17,8,7,7, 8,18,18,18,18,18,18,18,9,8, 9,19,19,19,19,19,19,19,19,10, 0,0,0,0,0,0,0,0,0,0, 11,21,20,20,20,20,20,20,20,20, 31,140,41,40,40,40,40,40,40,40, 51,160,160,61,60,60,60,60,60,60, 71,180,180,180,81,80,80,80,80,80, 91,200,200,200,200,101,100,100,100,100, 111,220,220,220,220,220,121,120,120,120, 131,240,240,240,240,240,240,141,140,140, 151,260,260,260,260,260,260,260,161,160, 171,280,280,280,280,280,280,280,280,181, 0,0,0,0,0,0,0,0,0,0, 192,301,300,300,300,300,300,300,300,300, 492,1600,601,600,600,600,600,600,600,600, 792,1900,1900,901,900,900,900,900,900,900, 1092,2200,2200,2200,1201,1200,1200,1200,1200,1200, 1392,2500,2500,2500,2500,1501,1500,1500,1500,1500, 1692,2800,2800,2800,2800,2800,1801,1800,1800,1800, 1992,3100,3100,3100,3100,3100,3100,2101,2100,2100, 2292,3400,3400,3400,3400,3400,3400,3400,2401,2400, 2592,3700,3700,3700,3700,3700,3700,3700,3700,2701, 0,0,0,0,0,0,0,0,0,0, 2893,4001,4000,4000,4000,4000,4000,4000,4000,4000, 6893,18000,8001,8000,8000,8000,8000,8000,8000,8000, 10893,22000,22000,12001,12000,12000,12000,12000,12000,12000, 14893,26000,26000,26000,16001,16000,16000,16000,16000,16000, 18893,30000,30000,30000,30000,20001,20000,20000,20000,20000, 22893,34000,34000,34000,34000,34000,24001,24000,24000,24000, 26893,38000,38000,38000,38000,38000,38000,28001,28000,28000, 30893,42000,42000,42000,42000,42000,42000,42000,32001,32000, 34893,46000,46000,46000,46000,46000,46000,46000,46000,36001, 0,0,0,0,0,0,0,0,0,0, 38894,50001,50000,50000,50000,50000,50000,50000,50000,50000, 88894,200000,100001,100000,100000,100000,100000,100000,100000,100000, 138894,250000,250000,150001,150000,150000,150000,150000,150000,150000, 188894,300000,300000,300000,200001,200000,200000,200000,200000,200000, 238894,350000,350000,350000,350000,250001,250000,250000,250000,250000, 288894,400000,400000,400000,400000,400000,300001,300000,300000,300000, 338894,450000,450000,450000,450000,450000,450000,350001,350000,350000, 388894,500000,500000,500000,500000,500000,500000,500000,400001,400000, 438894,550000,550000,550000,550000,550000,550000,550000,550000,450001, 0,0,0,0,0,0,0,0,0,0, 488895,600001,600000,600000,600000,600000,600000,600000,600000,600000, 1088895,2200000,1200001,1200000,1200000,1200000,1200000,1200000,1200000,1200000, 1688895,2800000,2800000,1800001,1800000,1800000,1800000,1800000,1800000,1800000, 2288895,3400000,3400000,3400000,2400001,2400000,2400000,2400000,2400000,2400000, 2888895,4000000,4000000,4000000,4000000,3000001,3000000,3000000,3000000,3000000, 3488895,4600000,4600000,4600000,4600000,4600000,3600001,3600000,3600000,3600000, 4088895,5200000,5200000,5200000,5200000,5200000,5200000,4200001,4200000,4200000, 4688895,5800000,5800000,5800000,5800000,5800000,5800000,5800000,4800001,4800000, 5288895,6400000,6400000,6400000,6400000,6400000,6400000,6400000,6400000,5400001, 0,0,0,0,0,0,0,0,0,0, 5888896,7000001,7000000,7000000,7000000,7000000,7000000,7000000,7000000,7000000, 12888896,24000000,14000001,14000000,14000000,14000000,14000000,14000000,14000000,14000000, 19888896,31000000,31000000,21000001,21000000,21000000,21000000,21000000,21000000,21000000, 26888896,38000000,38000000,38000000,28000001,28000000,28000000,28000000,28000000,28000000, 33888896,45000000,45000000,45000000,45000000,35000001,35000000,35000000,35000000,35000000, 40888896,52000000,52000000,52000000,52000000,52000000,42000001,42000000,42000000,42000000, 47888896,59000000,59000000,59000000,59000000,59000000,59000000,49000001,49000000,49000000, 54888896,66000000,66000000,66000000,66000000,66000000,66000000,66000000,56000001,56000000, 61888896,73000000,73000000,73000000,73000000,73000000,73000000,73000000,73000000,63000001, }; int bulin[20]={0,9,108,1107,11106,111105,1111104,11111103,111111102,1111111101}; int l,r; int num[2][10]; int wei[20],wn; void solve(int id,int x) { memset(wei,0,sizeof(wei)); wn=0; memset(num[id],0,sizeof(num[id])); if(x==0) { num[id][0]++; return ;} while(x) { wei[wn++]=x%10; x/=10; } reverse(wei,wei+wn); for(int i=0;i<wn;i++) { int row = (wn-i-1)*10+wei[i]; for(int j=0;j<10;j++) { num[id][j]+=no[row][j]; } } int res=0,e=0; for(int i=wn-1;i>0;i--) { if(i==wn-1) res=res+wei[i]; else res=res+wei[i]*(int)(pow(10,e)); e++; if(i-1>=0) num[id][wei[i-1]]+=res; } for(int i=1;i<wn;i++) { if(wei[i]) { int t = wn - i - 1; num[id][0]+=bulin[t]; } } } int main() { //freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout); while(scanf("%d%d",&l,&r)!=EOF) { if(l==0&&r==0) break; if(l>r) swap(l,r); l--; solve(1,r); solve(0,l); for(int i=0;i<10;i++) { printf("%d%c",num[1][i]-num[0][i],(i==9)?'\n':' '); } } return 0; }