The number of test instructions may only have 2,3,5 numbers called the ugly number output 1500th ugly number
STL Excellent Queue Application 1 is the ugly number of ugly number 2,3,5 times are ugly number with priority queue simulation on the line
#include <cstdio> #include <cstring> #include <set> #include <queue>using namespace Std;typedef Long long ll;//priority_queue<ll, vector<ll>, greater<ll> > Q;struct cmp{ bool Operator () (ll A, LL b) {return a > B;}}; Priority_queue<ll, Vector<ll>, cmp> q;set<ll> cnt;int ugly[3] = {2, 3, 5};int main () { ll A, B; Q.push (1); Cnt.insert (1); for (int i = 1;; ++i) { a = Q.top (); Q.pop (); if (i = =) break; for (int j = 0; j < 3; ++j) { b = a * ugly[j]; if (!cnt.count (b)) Cnt.insert (b), Q.push (b); } } printf ("The" th ugly number is%lld.\n ", a); return 0;}
Ugly numbers is numbers whose only prime factors is 2, 3 or 5. The sequence
1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, ...
Shows the first one ugly numbers. By convention, 1 is included.
Write a program to find and print the "th ugly number."
Input and Output
There is no. input to the program. Output should consist of a single line as shown below, with <number> replaced by the number computed.
Sample Output
The "th ugly number is <number>.
UVa 136 Ugly Numbers (priority queue)