Uva136 (priority queue)
Question:
The number that cannot be divided by prime numbers other than 2, 3, and 5, which is called the ugly number. Find 1,500th ugly numbers;
Ideas:
Use priority queue and map to determine the weight;
If x is ugly, 2x, 3x, and 5x are ugly;
Keep releasing the priority queue;
And retrieve the header (minimum number) x;
Determine whether the number has been accessed;
Find 1,500th outputs;
# Include
# Include
# Include
# Include
# Include
# Define ll long longusing namespace std; priority_queue
, Greater
> Q; map
M; int main () {q. push (1); m [1] = 1; int count = 0; while (1) {ll t = q. top (); q. pop (); count ++; if (count = 1500) {printf ("The 1500 'th uugly number is % lld. \ n ", t); break;} if (! M [t * 2]) {m [t * 2] = 1; q. push (t * 2);} if (! M [t * 3]) {m [t * 3] = 1; q. push (t * 3);} if (! M [t * 5]) {m [t * 5] = 1; q. push (t * 5) ;}} return 0 ;}