zoukankan      html  css  js  c++  java
  • 【UVa 136】Ugly Numbers

     Ugly Numbers 

    Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequence

    1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, ...

    shows the first 11 ugly numbers. By convention, 1 is included.

    Write a program to find and print the 1500'th ugly number.

    Input and Output

    There is no input to this program. Output should consist of a single line as shown below, with <number> replaced by the number computed.

    Sample output

    The 1500'th ugly number is <number>.

    嗯,好,这是一道比较直接的题目啊。

    大家可以直接百度一下。啦啦啦~~~~~~~

    开玩笑。

    现在有几个问题,找最小、判重。那这两个问题都是比较费时的。

    我们可以用小根堆+HASH,当然C++党可以用优先队列+SET。

    OK!现在问题都解决,直接上就行了。

    #include<cstdio>
    #include<cstring>
    #include<iostream>
    #include<vector>
    #include<set>
    #include<queue>
    using namespace std;
    
    typedef long long LL;
    set<LL> h;
    priority_queue<LL, vector<LL>, greater<LL> > heap;
    int orig[3] = {2, 3, 5};
    
    int main()
    {
        heap.push(1);
        h.insert(1);
        for (int i = 1;; ++i)
        {
            LL x = heap.top();
            heap.pop();
            if (i == 1500)
            {
                cout << "The 1500'th ugly number is " << x << ".
    ";
                break;
            }
            for (int j = 0; j < 3; ++j)
            {
                LL in = x * orig[j];
                if (!h.count(in))
                {
                    h.insert(in);
                    heap.push(in);
                }
            }
        }
        return 0;
    }
  • 相关阅读:
    OC与AS3的观察者模式比较以及外部回调
    判断矩形重叠
    2010谷歌校园招聘笔试题
    2011谷歌校园招聘笔试题
    2012Google校园招聘笔试题
    2013谷歌校园招聘笔试题2
    2013谷歌校园招聘笔试题
    2012搜狗校园招聘笔试题
    2013搜狗校园招聘笔试题
    搜狐面试题
  • 原文地址:https://www.cnblogs.com/albert7xie/p/4731213.html
Copyright © 2011-2022 走看看