Uva136——Ugly Numbers

来源:互联网 发布:上位机软件开发 编辑:程序博客网 时间:2024/06/04 23:25
#include <iostream>#include <vector>#include <queue>#include <set>#include <cstdio>using namespace std;typedef long long LL;const int num[3] = {2, 3, 5};int main(){priority_queue<LL, vector<LL>, greater<LL> > que;set<LL> s;que.push(1);s.insert(1);for(int i = 1; ; i++){LL x = que.top(); que.pop();if(i == 1500){cout << "The 1500'th ugly number is " << x << ".\n";break;}for(int j = 0; j < 3; j++){LL x2 = x * num[j];if(!s.count(x2)){s.insert(x2);que.push(x2);}}}return 0;}

0 0
原创粉丝点击