Hdu1058 humble numbers (ugly)

Source: Internet
Author: User

Ugly number: numbers containing only certain quality factors are called ugly numbers, for example, including 2, 3, and 5. let's take 2, 3, 4, 5, 6, 8, 9, 10, 12, 15 ........ But we usually call 1 as the first ugly number.

Solution: the question we are doing now is to take, as the qualitative factor, and ask us to calculate the N ugly number (1 as the first ugly number ), it can be solved using the DP idea. We first take array [1] = 1 as the basis, multiply it by the prime factor to obtain the second number of the ordered array, and so on. Write the state transition equation: array [N] = min (array [n2] * 2, array [N3] * 3, array [N5] * 5, array [N7] * 7 ); this is the idea of solving this question. Paste the Code:
# Include <stdio. h> int min (INT num1, int num2, int num3, int num4) // find the smallest one of the four {int min1, min2; min1 = num1> num2? Num2: num1; min2 = num3> num4? Num4: num3; Return (min1> min2? Min2: min1);} void uugly (INT array []) // obtain the ugly numbers in the order {array [1] = 1; int mark; int N, n2 = 1, N3 = 1, N5 = 1, N7 = 1; for (n = 2; n <= 5842; n ++) {mark = min (array [n2] * 2, array [N3] * 3, array [N5] * 5, array [N7] * 7 ); // state transition equation array [N] = mark; If (Mark % 2 = 0) // if it is a multiple of the quality factor, add 1 N2 ++; if (Mark % 3 = 0) N3 ++; If (Mark % 5 = 0) N5 ++; If (Mark % 7 = 0) n7 ++ ;}} void output (INT array [], int N) // control of the output format {If (N % 10 = 1 & N % 100! = 11) printf ("the % DST humble number is % d. \ n ", N, array [N]); else if (N % 10 = 2 & amp; n % 100! = 12) printf ("the % DND humble number is % d. \ n ", N, array [N]); else if (N % 10 = 3 & amp; n % 100! = 13) printf ("the % DRD humble number is % d. \ n ", N, array [N]); else printf (" the % DTH humble number is % d. \ n ", N, array [N]);} int main () {int N; int array [6000]; uugly (array ); while (scanf ("% d", & N )! = EOF & n! = 0) {output (array, n);} return 0 ;}

 

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.