HDU1058:Humber Numbers(DP)

Humble Numbers
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 30872 Accepted Submission(s): 13503

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.

这题因为测试数据多,肯定先要打个1~5842(或以上一点点)的表,不然会超时。
打表也不像之前的DP有选不选的问题,感觉这题的打表就是一点点往上填。值得注意的一点是当dp[q2]*2 , dp[q3]*3,dp[q5]*5,dp[q7]*7中任意几项相等的时候,比如dp[q2]*2和dp[q3]*3的这两项相等,一定要让q2和q3都++,不然会出现dp[6] = 6,dp[7] = 6的尴尬局面,所以while里面判断的时候不能用else if ,都要用if。

总结一下鄙人wa的的错误:

  1. 输入0时终止输入没写。 //切记切记
  2. 主函数里写判断条件时n % 100 != 13时写成12。

下面附上ac代码:

#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <ctype.h>
#include <queue>
#include <cmath>
#define INF 0x3f3f3f3f
#define mod 1000000007
using namespace std;
typedef long long int ll;
ll dp[6000];
int main() {
    dp[1] = 1;
    int q2 = 1,q3 = 1,q5 = 1,q7 = 1;
    int cnt = 1; //记录下标
    int temp; //暂存四个数中的最小值
    while(cnt < 5900) { //打表
        int a = 2*dp[q2];
        int b = 3*dp[q3];
        int c = 5*dp[q5];
        int d = 7*dp[q7];
        temp = min(min(a,b),min(c,d));
        if(temp == a) {  //需要注意的是若a =or= b =or= c =or= d
            q2 ++;       //所以不能用else if...
        }
        if(temp == b) {
            q3 ++;
        }
        if(temp == c) {
            q5 ++;
        }
        if(temp == d) {
            q7++;
        }
        dp[++cnt] = temp; //下标加一后存数
    }
    int n;
    while(cin >> n) {
        if(n == 0) {break;} //切记切记 千万别忘了
        if(n % 10 == 1 && n % 100 != 11) {
            cout << "The " << n << "st humble number is " << dp[n] << "." << endl;
        }
        else if(n % 10 == 2 && n % 100 != 12) {
            cout << "The " << n << "nd humble number is " << dp[n] << "." << endl;
        }
        else if(n % 10 == 3 && n % 100 != 13) {
            cout << "The " << n << "rd humble number is " << dp[n] << "." << endl;
        }
        else {
            cout << "The " << n << "th humble number is " << dp[n] << "." << endl;
        }
    }
    return 0;
}


猜你喜欢

转载自blog.csdn.net/qq_43555854/article/details/86634924