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優先佇列最小堆最大堆佇列
- STL(二十)priority_queue優先佇列容器佇列
- C++ STL 優先佇列 (priority_queue)C++佇列
- PHP優先佇列PHP佇列
- 堆--優先佇列佇列
- 優先佇列 (轉)佇列
- 找最小的k個數(優先佇列)佇列
- 淺談優先佇列佇列
- 堆與優先佇列佇列
- 堆和優先佇列佇列
- 優先佇列和堆排序佇列排序
- 堆排序與優先佇列排序佇列
- Java優先佇列(PriorityQueue)示例Java佇列
- 01揹包優先佇列優化佇列優化
- hdu 4546 優先佇列 數列組合和第m小佇列
- 棧,佇列,優先順序佇列簡單介面使用佇列
- Redis實現任務佇列、優先順序佇列Redis佇列
- NO GAME NO LIFE(優先佇列/最小堆)GAM佇列
- 優先佇列的比較器佇列
- 封裝優先順序佇列封裝佇列
- 二叉堆優先佇列佇列
- POJ 3253 Fence Repair 優先佇列AI佇列
- 堆——神奇的優先佇列(上)佇列
- 優先佇列的效能測試佇列
- hdu5040 優先佇列+bfs佇列
- 佇列 優先順序佇列 python 程式碼實現佇列Python
- 演算法面試(三) 優先佇列演算法面試佇列
- 【圖論】拓撲排序+優先佇列圖論排序佇列
- 1007(優先佇列)佇列
- POJ 1724 ROADS(優先佇列+spfa)佇列
- POJ2431 Expedition (優先佇列)佇列
- POJ 2051(最小堆/優先佇列)佇列
- 三、資料結構演算法-棧、佇列、優先佇列、雙端佇列資料結構演算法佇列
- STL(十九)queue佇列容器佇列
- .NET 6 優先佇列 PriorityQueue 實現分析佇列
- Java優先順序佇列DelayedWorkQueue原理分析Java佇列
- 二叉堆實現優先佇列佇列