Problem Link: UVA136 Ugly Numbers. basic level exercises, written in C + + language program.
Test Instructions Summary: Can not be 2, 3 and 5 is not divisible by the number of primes called the ugly number, find the 1500th ugly number.
Problem analysis: In other words, the number of ugly factors can only be 2, 3, and 5. 1 is the number of ugly, for x, if x is the number of ugly 2x, 3x and 5x is the number of ugly. Using the known ugly number, from small to continuous generation of ugly number can be.
program, use an STL container set to store the number of ugly. The collection has the function of de-duplication and automatic sorting, which is convenient for solving this problem.
The AC C + + language program is as follows:
/* UVA136 Ugly Numbers * * #include <iostream> #include <cstdio> #include <set>using namespace std;# Define MAXN 1500typedef unsigned long long ull;set<ull> ugly;int main (void) { int count; Ugly.insert (1); Count = 0; Set<ull>::iterator iter = Ugly.begin (); while (++count < MAXN) { ULL t = *iter; Ugly.insert (T * 2); Ugly.insert (T * 3); Ugly.insert (T * 5); iter++; } printf ("The" th ugly number is%llu.\n ", *iter); return 0;}
UVA136 Ugly Numbers