1. 程式人生 > >HDU 1058 Humble Numbers (動規+尋找醜數問題)

HDU 1058 Humble Numbers (動規+尋找醜數問題)

string ffi 找規律 得到 mar lan algorithm size cep

Humble Numbers

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 16742 Accepted Submission(s): 7280



Problem Description A number whose only prime factors are 2,3,5 or 7 is called a humble number. The sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 14, 15, 16, 18, 20, 21, 24, 25, 27, ... shows the first 20 humble numbers.

Write a program to find and print the nth element in this sequence

Input The input consists of one or more test cases. Each test case consists of one integer n with 1 <= n <= 5842. Input is terminated by a value of zero (0) for n.

Output For each test case, print one line saying "The nth humble number is number.". Depending on the value of n, the correct suffix "st", "nd", "rd", or "th" for the ordinal number nth has to be used like it is shown in the sample output.

Sample Input
1
2
3
4
11
12
13
21
22
23
100
1000
5842
0

Sample Output
The 1st humble number is 1.
The 2nd humble number is 2.
The 3rd humble number is 3.
The 4th humble number is 4.
The 11th humble number is 12.
The 12th humble number is 14.
The 13th humble number is 15.
The 21st humble number is 28.
The 22nd humble number is 30.
The 23rd humble number is 32.
The 100th humble number is 450.
The 1000th humble number is 385875.
The 5842nd humble number is 2000000000.

Source University of Ulm Local Contest 1996
Recommend JGShining | We have carefully selected several similar problems for you: 1176 1421

pid=1024" target="_blank">1024 1025 1081

大神找規律,我這樣的弱菜僅僅能看別人代碼了。。orz。
分別乘2,乘3,乘5。乘7。哪個小取哪個,乘過之後,p2,p3,p5。p7的值還要改變一下。。。

事實上就是一個光搜的過程,,,。題目。

每一個數都能夠分解成有限個2 3 5 7 的乘積,dp方程為dp[i]=f[i]=min(f[a]*2,min(f[b]*3,min(f[c]*5,f[d]*7)))

找到比f[i-1]大且最小的數。在這裏用到了滾動查找;

以下詳解:

a表示f[]數組中,下標為a的數*2 可能得到當前的 f[i];若是則++

b表示f[]數組中,下標為b的數* 3 可能得到當前的f[i];若是則++

c表示f[]數組中,下標為b的數* 5 可能得到當前的f[i];若是則++

d表示f[]數組中,下標為b的數* 7 可能得到當前的f[i];若是則++

求出他們中的min,則為f[i];


 假設一個數的質因子僅僅有2357。那麽這個數被稱為Humble Numbers(差數)。

將正整數正序排列(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 14, 15, 16, 18, 20, 21, 24, 25, 27, ... )你會發現前20Humble Numbers(差數)。 如今,你的任務是編寫一個程序將第nHumble Numbers(差數)找出並打印出來。 輸入規範: 輸入的數有很多組。當中的n均滿足1 <= n <= 5842。假設輸入0,則表示終止。 輸出規範: 對於每一組數。分列幾行輸出“The nth humble number is number.”,當中nth必須依據英語詞法規範寫為“1st2nd3rd4th5th6th7th8th9th10th... ”。

還用到英語。

。。orz。
一般來說個位數是1為st,個位數為2是nd。個位數為3是rd;可是有例外。11。 12 ,13均為th,同一時候111,112,113均為th。除此之外,其它都是th;
代碼:

#include <iostream>
#include <algorithm>
using namespace std;
#define M 50000
__int64 min(__int64 a,__int64 b,__int64 c,__int64 d){  
    __int64 x=a<b?a:b;  
    __int64 y=c<d?c:d;  
    return x<y?x:y;  
}  
__int64 vis[M];
int main(__int64 p2,__int64 p3,__int64 p5,__int64 p7)
{   
    int i=1,n,cur;
    p2=p3=p5=p7=1;
    memset(vis,0,sizeof(vis)); vis[1]=1;
    while(vis[i]<=2000000000)
    {
        vis[++i]=min(vis[p2]*2,vis[p3]*3,vis[p5]*5,vis[p7]*7);
        if(vis[i]==vis[p2]*2) p2++;  
        if(vis[i]==vis[p3]*3) p3++;  
        if(vis[i]==vis[p5]*5) p5++;  
        if(vis[i]==vis[p7]*7) p7++;  
    }
    while(scanf("%d",&n)!=EOF,n)
    {
        string ss;  
        if(n%10==1&&n%100!=11)  
            printf("The %dst humble number is %I64d.\n",n,vis[n]);  
        else if(n%10==2&&n%100!=12)  
            printf("The %dnd humble number is %I64d.\n",n,vis[n]);  
        else if(n%10==3&&n%100!=13)  
            printf("The %drd humble number is %I64d.\n",n,vis[n]);  
        else   
            printf("The %dth humble number is %I64d.\n",n,vis[n]);  
        
    }  
return 0;
}

HDU 1058 Humble Numbers (動規+尋找醜數問題)