Poj1338 uugly numbers

Source: Internet
Author: User

Question:
Calculate the number of numbers with a prime number of 2, 3, and 5, including 1.
Analysis:
As long as you want to obtain 1,500th, you can use the table-making method to obtain all the first 1500 numbers,
Because each ugly number can be decomposed by a previous number, it is not necessary to search
2, 3, 5 multiplied by a number that is exactly greater than the current maximum ugly, which is the latest ugly number.
SeeCode:

# Include <iostream>
Using namespace STD;
# Define x 1505
Int Uggly [X]; // records the first 1500 Uggly numbers.
Int tot;
Void solve () // search function
{
Int tot = 0;
Int P2 = 1, P3 = 1, P5 = 1; // represents the position where the current number of 2 3 5 is multiplied by the number of ugly s that are exactly greater than the maximum number of ugly S.
Uugly [++ tot] = 1;
While (TOT <= 1500)
{
While (2 * Uggly [P2] <= Uggly [tot]) // until the position of the Uggly number that is exactly greater than the maximum is found
P2 ++;
While (3 * Uggly [P3] <= Uggly [tot]) // likewise
P3 ++;
While (5 * Uggly [P5] <= Uggly [tot]) // likewise
P5 ++;
// Assign the minimum value of the above three numbers to the latest ugly number
Uggly [++ tot] = min (2 * Uggly [P2], min (3 * Uggly [P3], 5 * Uggly [P5]);
}
}
Int main ()
{
Freopen ("sum. In", "r", stdin );
Freopen ("sum. Out", "W", stdout );
Int N;
Solve ();
While (CIN> N, N)
Cout <uugly [N] <Endl;
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.