STL醜數(set+優先佇列)
Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequence
1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, ...
shows the first 11 ugly numbers. By convention, 1 is included.
Write a program to find and print the 1500’th ugly number.
Input
There is no input to this program.
Output
Output should consist of a single line as shown below, with ‘’ replaced by the numbercomputed.
Sample Output
The 1500'th ugly number is .
問題連結:UVA136 Ugly Numbers。
set做法
注意while迴圈中不能用ugly.size()作為迴圈控制條件,這樣會使有的元素還沒有加入到集合中
#include <iostream>
#include <cstdio>
#include <set>
using namespace std;
#define MAXN 1500
typedef unsigned long long ULL;
set<ULL> ugly;
int main(void)
{
int count;
ugly.insert(1);
count=0;
set<ULL>::iterator iter = ugly.begin();//迭代器傳送
while(++count<MAXN)
{
int t=*iter;
ugly.insert(t*2);
ugly.insert(t*3);
ugly.insert(t*5);
iter++;
}
printf("The 1500'th ugly number is %llu.\n", *iter);
return 0;
}
優先佇列
#include<iostream>
#include<vector>
#include<queue>
#include<set>
using namespace std;
typedef long long LL;
const int coeff[3] = {2, 3, 5};
int main()
{
priority_queue<LL, vector<LL>, greater<LL> > pq;
set<LL> s;
pq.push(1);
s.insert(1);
for(int i = 1; ; i++)
{
LL x = pq.top(); pq.pop();
if(i == 1500)
{
cout << "The 1500'th ugly number is " << x << ".\n";
break;
}
for(int j = 0; j < 3; j++)
{
LL x2 = x * coeff[j];
if(!s.count(x2))
{
s.insert(x2);
pq.push(x2);
}
}
}
return 0;
}
相關文章
- STL 優先佇列 用法佇列
- STL優先佇列最小堆最大堆佇列
- C++ STL 優先佇列 (priority_queue)C++佇列
- PHP優先佇列PHP佇列
- 淺談優先佇列佇列
- 堆與優先佇列佇列
- 優先佇列和堆排序佇列排序
- 01揹包優先佇列優化佇列優化
- 佇列 優先順序佇列 python 程式碼實現佇列Python
- 棧,佇列,優先順序佇列簡單介面使用佇列
- NO GAME NO LIFE(優先佇列/最小堆)GAM佇列
- 優先佇列的比較器佇列
- 封裝優先順序佇列封裝佇列
- 三、資料結構演算法-棧、佇列、優先佇列、雙端佇列資料結構演算法佇列
- leetcode621——優先佇列的思路LeetCode佇列
- 演算法面試(三) 優先佇列演算法面試佇列
- 二叉堆實現優先佇列佇列
- 手擼優先佇列——二叉堆佇列
- .NET 6 優先佇列 PriorityQueue 實現分析佇列
- Java優先順序佇列DelayedWorkQueue原理分析Java佇列
- 堆、堆排序和優先佇列的那些事排序佇列
- 最詳細版圖解優先佇列(堆)圖解佇列
- 【堆】【優先佇列】[NOIP2004]合併果子佇列
- Facebook的分散式優先順序佇列FOQS分散式佇列
- 8.13(優先佇列貪心維護+打表找規律+對頂堆優先佇列+DFS減枝+貪心dp)佇列
- 【Dijkstra演算法】未優化版+優先佇列優化版演算法優化佇列
- codeforces round 974(div.3)E(優先佇列實現dijstra演算法,devc++的優先佇列用greater報錯)佇列JS演算法devC++
- CodeForces - 960B:Minimize the error(優先佇列+貪心)Error佇列
- Sunscreen POJ - 3614(防曬油) 貪心-優先佇列佇列
- [USACO 2009 Feb Gold] Fair Shuttle (貪心+優先佇列)GoAI佇列
- 牛客網 複數集合(小根堆的優先佇列、北郵機試)佇列
- CSP之公共鑰匙盒(模擬、排序、優先佇列)排序佇列
- Python3 執行緒優先順序佇列( Queue)Python執行緒佇列
- MaxHeap 最大堆 MinHeap 最小堆 PriorityQueue 優先佇列實現佇列
- Python 列表推導及優先順序佇列的實現Python佇列
- 個推基於 Apache Pulsar 的優先順序佇列方案Apache佇列
- RMQ——支援合併和優先順序的訊息佇列MQ佇列
- 資料結構之PHP(最大堆)實現優先佇列資料結構PHP佇列
- 1284 海港 普及組 NOIP2016 佇列基礎 簡單列舉 簡單模擬 優先佇列(priority_queue)佇列