hdu1058 Humble NUMBERS--DP

Source: Internet
Author: User

Original title Link: http://acm.hdu.edu.cn/showproblem.php?pid=1058


One: The original question content

Problem Description A number whose only prime factors are 2,3,5/7 is called a humble number. The sequence is 1, 2, 3, 4, 5, 6, 7, 8, 9, and, A,,, and, shows the numbers of the "humble".

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. The Input is terminated by a value of zero (0) for N.
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 is used like I T is shown in the sample output.
Sample Input

1 2 3 4 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. Second: Analysis and understanding

The whole idea is to use the elements in the known sequence to generate new elements according to the rules, such as X * 2, X * 3, X * 5, X * 7.
Suppose you use array A[max] to store all the elements of this sequence, first make a[0] = 1, which means that the first element is 1, then use 4 pointers (not memory pointers, hehe), i2 = i3 = i5 = i7 = 0. Each time we compare A[I2] * 2, A[I3] * 3, A[I5] * 5, A[i7] * 7 The size of the four elements, put the smallest into the sequence, and put the corresponding pointer +1 until the number of required numbers is generated.


Three: AC code

#include <iostream>  
#include <string.h>
#include <algorithm>  

using namespace std;

int a[6000];

int main ()
{
	a[1] = 1;
	int I2, i3, i5, i7;
	I2 = i3 = i5 = i7 = 1;

	for (int i = 2; I <= 5842 i++)
	{
		int x = min (min (a[i2) * 2, A[i3] * 3), MIN (a[i5] * 5, A[i7] * 7));

		if (x = = A[i2] * 2) i2++;//This place cannot use the If...else statement, because the comparison of four numbers may exist equal
		if (x = = A[i3] * 3) i3++;
		if (x = = a[i5] * 5) i5++;
		if (x = = A[i7] * 7) i7++;

		A[i] = x;
	}

	int n;

	while (scanf ("%d", &n) && N)
	{
		if (n% = 1 && n%!=)
			printf ("The%DST HUMBL E number is ", n);
		else if (n% = 2 && n%!=)
			printf ("The%DND humble number is", n);
		else if (n% = 3 && n%!=)
			printf ("The%DRD humble number is", n);
		else
			printf ("The%dth humble number is", n);

		printf ("%d.\n", A[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.