程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> 關於C++ >> HDOJ 1663 The Counting Problem 打表

HDOJ 1663 The Counting Problem 打表

編輯:關於C++



先打出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 2004 Asia Regional Shanghai

/* ***********************************************
Author        :CKboss
Created Time  :2015年02月01日 星期日 13時58分50秒
File Name     :HDOJ1663_3.cpp
************************************************ */

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

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;i0;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;ir) 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;
}



  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved