zoukankan      html  css  js  c++  java
  • nyoj1032——Save Princess——————【set应用】

    Save Princess

    时间限制:1000 ms  |  内存限制:65535 KB
    难度:2
     
    描述
    Yesterday, the princess was kidnapped by a devil. The prince has to rescue our pretty princess.
     
    "OK, if you want to save the beautiful princess, you must answer my questions correctly."the devil says.
     
    "No problem!".
     
    "I’ll ask you t questions. For each question, I’ll tell you an integer n, you must tell me the i th beatuiful number. If your answer is wrong, the princess and you will all die".
     
    "But what is the characteristic of the beautiful number?" Pince asks.
     
    "Beautiful numbers are numbers whose only prime factors are 2, 3 or 5. The sequence 
    1, 2, 3, 4, 5, 6, 8, 9, 10, ...   shows the first 9 beautiful numbers.  
    By convention, 1 is included. "
     
    Can you help the prince to save the princess?
     
    输入
    The input for each case is an integer n(1≤n≤5000) and it is terminated by a negative integer.
    输出
    For each test case, you should print an integer which represents the i th beautiful number.
    样例输入
    2
    3
    -1
    样例输出
    2
    3


    解题思路:用优先队列来存放美丽数,每次从队列中拿出最小的美丽数,由它扩展出三个美丽数,用set的去重功能来判断是否放入set和优先队列。依次从优先队列中出来的就是从小到大的美丽数。

    #include<stdio.h>
    #include<string.h>
    #include<set>
    #include<queue>
    #include<vector>
    #include<algorithm>
    using namespace std;
    typedef long long LL;
    const int f[4]={2,3,5};
    LL a[5500];
    void prin(int n){
    
        priority_queue<LL,vector<LL>,greater<LL> >pq;
        set<LL>s;
        pq.push(1);
        s.insert(1);
        for(int i=1;;i++){
    
            LL tm=pq.top();
            a[i]=tm;
            pq.pop();
            if(i>n)
                break;
            for(int i=0;i<3;i++){
    
                LL x=tm*f[i];
                if(!s.count(x)){//返回元素x的个数
    
                    s.insert(x);
                    pq.push(x);
                }
            }
        }
    
    }
    int main(){
    
        int n;
        prin(5010);
        while( scanf("%d",&n)!=EOF&&n>0){
    
            printf("%lld
    ",a[n]);
        }
        return 0;
    }
    

      

  • 相关阅读:
    一个例子看懂所有nodejs的官方网络demo
    HDU 4283 You Are the One ★(进出栈的括号匹配性质:区间DP)
    HDU 4283 You Are the One ★(进出栈的括号匹配性质:区间DP)
    HDU 4274 Spy's Work (树形DP)
    HDU 4274 Spy's Work (树形DP)
    HUST 1328 String (字符串前缀子串个数 --- KMP)
    HUST 1328 String (字符串前缀子串个数 --- KMP)
    POJ 3167 Cow Pattern ★(KMP好题)
    POJ 3167 Cow Pattern ★(KMP好题)
    HDU 2594 Simpsons’ Hidden Talents (KMP)
  • 原文地址:https://www.cnblogs.com/chengsheng/p/4385397.html
Copyright © 2011-2022 走看看