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;
}