• 丑数(摘)


    丑数是指不能被2,3,5以外的其他素数整除的数,把丑数从小到大排列起来,结果如下:

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

    求第n个丑数

    输入n

    #include<iostream>
    #include<queue>
    #include<vector>
    #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);
        int n;
        cin>>n;
        for(int i=1;;i++){
            LL x=pq.top();
            pq.pop();
            if(i==n){
                cout<<"The "<<n<<"'th ugly number is "<<x<<".
    ";
                break;
            }
            for(int j=0;j<3;j++){
                LL x2=x * coeff[j];
                if(!s.count(x2)){             //计数x2,不为0则存入队列中
                    s.insert(x2);
                    pq.push(x2);
                }
            }
        }
        system("pause");
        return 0;
    }

    因为queue仅为FIFO结构,不能说为一个容器,无count函数,所以用set存储来计数看是否有重复元素

  • 相关阅读:
    python——numpy (二)
    python——numpy(一)
    python——matplotlib
    redis
    图片验证码识别技术——Tesseraact
    Linux 环境变量PROMPT_COMMAND
    maven项目管理工具
    Log4J日志组件
    java中的泛型,注解
    数据库备份还原
  • 原文地址:https://www.cnblogs.com/farewell-farewell/p/5245763.html
Copyright © 2020-2023  润新知