HDU 1058 humble numbers

Source: Internet
Author: User

Now that we classify this question into the dynamic planning questions, we start by thinking about it with the idea of dynamic planning.

There was no breakthrough in the morning.

Define four pseudo pointers to store the subscript, indicating the subscript of the smallest number that can be multiplied by 2, 3, 5, and 7 in the known ugly number.

The above sentence should be well understood.

That is to say, DP [P1] * 2 can get a new ugly number, and the other three are like this.

But what we need is the next ugly number in the largest known ugly number, that is, the minimum value of the new ugly number.

That is, min (DP [P1] * 2, DP [P2] * 3, DP [P3] * 5, DP [P4] * 7 );

 

In addition, it should be noted that in dp (), when the ugly numbers generated by one or two are the same as the minimum values, the corresponding "Pointer" must also be auto-incremented by 1.

For example, when I = 5, P1 = 3, P2 = 2, DP [P1] * 2 = DP [P2] * 3 = 6;

If you only execute ++ P1 and do not execute ++ P2, the ugly number 9 is missing.

 

Finally, I spoke about the egg pain output ..

When can I ignore the question, I think about DP question a by myself ..

 

 1 //#define LOCAL 2 #include <iostream> 3 #include <cstdio> 4 #include <cstring> 5 #include <algorithm> 6 using namespace std; 7  8 int dp[5850]; 9 10 inline int min(int a, int b, int c, int d)11 {12     a = min(a, b);13     c = min(c, d);14     return min(a, c);15 }16 17 void DP(void)18 {19     int i;20     int p1 = 1, p2 = 1, p3 = 1, p4 = 1;21     dp[1] = 1;22     for(i = 2; i <= 5842; ++i)23     {24         dp[i] = min(dp[p1] * 2, dp[p2] * 3, dp[p3] * 5, dp[p4] * 7);25         if(dp[i] == dp[p1] * 2)26             ++p1;27         if(dp[i] == dp[p2] * 3)28             ++p2;29         if(dp[i] == dp[p3] * 5)30             ++p3;31         if(dp[i] == dp[p4] * 7)32             ++p4;33     }34 } 35 36 int main(void)37 {38     #ifdef LOCAL39         freopen("1058in.txt", "r", stdin);40     #endif41 42     DP();43     int n;44     while(scanf("%d", &n) && n)45     {46         if(n % 10 == 1 && n % 100 != 11)47             printf("The %dst humble number is ", n);48         else if (n % 10 == 2 && n % 100 != 12)49         {50             printf("The %dnd humble number is ", n);51         }52         else if (n % 10 == 3 && n % 100 != 13)53         {54             printf("The %drd humble number is ", n);55         }56         else57             printf("The %dth humble number is ", n);58 59         printf("%d.\n", dp[n]);60     }61     return 0;62 }
Code Jun

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.