POJ1338丑数(uva136)

本文详细解析了一道UVA和POJ的编程题目,作者使用了vector替代UVA书中采用的set,展示了如何利用C++ STL中的vector和priority_queue解决数学序列生成问题,通过代码实例对比了两种数据结构在实际应用中的表现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

UVA书上用的set,poj上这个题我用的vector。

 1 #include <iostream>
 2 #include <string>
 3 #include <vector>
 4 #include <algorithm>
 5 #include <cstdio>
 6 #include <cstdlib>
 7 #include <cstring>
 8 #include <queue>
 9 #include <cstdio>
10 #include <cstdlib>
11 #include <set>
12 #include <sstream>
13 
14 using namespace std;
15 const int a[3]={2,3,5};
16 typedef long long LL;
17 
18 int main()
19 {
20     priority_queue<LL, vector<LL>,greater<LL> > pq;
21     vector<LL> s;
22     pq.push(1);
23     s.push_back(1);
24     int i,j,m;
25     for(i=1;;i++)
26     {
27         LL y = pq.top();
28         pq.pop();
29         if(i==1500)break;
30         for(j=0;j<3;j++)
31         {
32             LL x = y*a[j];
33             if(find(s.begin(),s.end(),x)==s.end())
34             {
35                 s.push_back(x);
36                 pq.push(x);
37             }
38         }
39     }
40     sort(s.begin(),s.end());
41     while(cin >> m && m)
42         cout << s[m-1] << endl;
43     return 0;
44 }

 

转载于:https://www.cnblogs.com/HsiaoYeekwan/p/6345083.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值