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;
    }
  • 相关阅读:
    lodash kebabCase
    lodash escapeRegExp 转义正则特殊字符
    lodash capitalize 首字母大写
    lodash camelCase 驼峰写法
    lodash pick
    lodash random
    lodash round
    Linux 目录结构
    每天一个linux命令(6/18):lsof命令
    Linux 内核编译步骤及配置详解
  • 原文地址:https://www.cnblogs.com/albert7xie/p/4731213.html
Copyright © 2011-2022 走看看