UVa 136 Ugly Numbers (priority queue)

Source: Internet
Author: User

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

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)

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.