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\n",a[n]);
    }
    return 0;
}
nyoj1032——Save Princess——————【set应用】
原文:http://www.cnblogs.com/chengsheng/p/4385397.html