poj 1338 Ugly Numbers

本文通过一个具体的程序示例介绍了如何使用优先队列解决特定问题。该程序利用C++中的优先队列实现了一个算法,用于生成一系列数值。此外,文章还提供了一个完整的代码示例,演示了从初始化到主函数调用的整个过程。

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

 原题链接:http://poj.org/problem?id=1338

优先队列的应用,如下:

 1 #include<cstdio>
 2 #include<cstdlib>
 3 #include<queue>
 4 #include<functional>
 5 #include<iostream>
 6 #include<algorithm>
 7 using namespace std;
 8 typedef long long ll;
 9 const int Max_N = 1510;
10 ll ans[Max_N];
11 void init(){
12     int arr[4] = { 2, 3, 5 };
13     priority_queue<ll, vector<ll>, greater<ll> > que;
14     que.push(1);
15     for (int i = 1; i <= 1500;){
16         ll t = que.top();
17         que.pop(), ans[i] = t;
18         if (ans[i] == ans[i - 1]){
19             continue;
20         } else {
21             i++;
22         }
23         for (int j = 0; j < 3; j++) que.push((ll)(t * arr[j]));
24     }
25 }
26 int main(){
27     init();
28     int n;
29     while (~scanf("%d", &n) && n) printf("%lld\n", ans[n]);
30     return 0;
31 }
View Code

 

转载于:https://www.cnblogs.com/GadyPu/p/4462698.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值