zoukankan      html  css  js  c++  java
  • UVA136 Ugly Numbers【set】【优先队列】

                                                       丑数

     题目大意:

    丑数是指不能被2,3,5以外的其他素数整除的数。把丑数从小到大排列起来,结果如下: 1,2,3,4,5,6,8,9,10,12,15,… 求第1500个丑数。

     

    提示:从小到大生成各个丑数。最小的丑数是1,对于任意丑数x,2x,3x和5x也是丑数。使用一个优先队列保存已生成的丑数,每次取出最小的丑数,生成3个新的丑数。需要注意,同一个丑数有多种生成方式,所以需要判断一个丑数是否已经生成过。

    题目链接:https://vjudge.net/contest/211547#problem/G

    #include<iostream>
    #include <functional>             //包含greater
    #include<vector>
    #include<queue>
    #include<set>
    using namespace std;
    typedef long long LL;
    const int coeff[3] = { 2, 3, 5 };
    
    int main() {
        priority_queue<LL, vector<LL>, greater<LL> > pq;
        set<LL> s;
        pq.push(1);
        s.insert(1);
        for (int i = 1; ; i++) {
            LL x = pq.top(); pq.pop();
            if (i == 1500) {
                cout << "The 1500'th ugly number is " << x << ".
    ";
                break;
            }
            for (int j = 0; j < 3; j++) {
                LL x2 = x * coeff[j];
                if (!s.count(x2)) { s.insert(x2); pq.push(x2); }
            }
        }
        return 0;
    }

    2018-03-25  18:41:35


    作者:is_ok
    出处:http://www.cnblogs.com/00isok/
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

  • 相关阅读:
    开淘店记录
    广告轮播效果
    loading事件加载效果
    正则表达式摘要
    cookie存取数据分析
    js 空格与回车处理
    数据对象型转换为数组型
    变换闪烁效果
    eclipse配置新环境
    五小时轻松入门Python
  • 原文地址:https://www.cnblogs.com/00isok/p/8645903.html
Copyright © 2011-2022 走看看