例题 5-7 丑数(Ugly Numbers,UVa 136)

题意:

丑数是一些因子只有2,3,5的数。数列1,2,3,4,5,6,8,9,10,12,15……写出了从小到大的前11个丑数,1属于丑数。现在请你编写程序,找出第1500个丑数是什么。

思路:

如果按照正向思维分析,需要考虑除2,3,5以外的所有素数–这显然不切实际。
因此考虑派生的性质:假设一个丑数为x,那么2x,3x,5x也都是丑数。
我们知道1是最小的丑数,因此从1开始,从小到大依次向后派生新丑数即可

并且在紫书里有强行推荐使用优先队列解的样子啊,明明set就可以做了

#include<bits/stdc++.h>
using namespace std;
int main() {
	set<long long> s; s.insert(1);
	auto it = s.begin(); 
	for(int i = 0; i < 1500; i++){
		long long x =  *it;
		s.insert(x*2); s.insert(x*3); s.insert(x*5);   
		it++; 
	}
	it--;
	cout << "The 1500'th ugly number is " << *it << "." << endl; 
	return 0;
} 

收获:
1、派生法求第n个x数。
2、count()函数去重。

原文地址:https://www.cnblogs.com/RioTian/p/12987136.html