STL UVA-136 Ugly Numbers 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 number computed. Sample Output The 1500'th ugly number is .

丑数,因子只有2、3、5,一开始每个数都除到1的辣鸡方法,自然是T了。(其实最最最开始是读错题了hhh

后来就从前往后递推这样子(很多地方都有用到这样的方法叭

(中间还用过愚蠢而没有必要的优先队列

放入set里,自动排序,自动去重,简直如同父母的乖宝宝

最后一直WA因为没有打换行符???!心态崩了,问问问.jpg

#include <cstdio>
#include <queue>
#include <set>
using namespace std;

int main()
{
	set <long long> s;
	s.insert(1);
	set <long long>::iterator it=s.begin();
	long long i=0,x;

	while(i<1500){
		x=*it;
		s.insert(x*2);
		s.insert(x*3);
		s.insert(x*5);
		it++;i++;
	}
	printf("The 1500'th ugly number is %lld.\n",x);

	return 0;
}

猜你喜欢

转载自blog.csdn.net/DADDY_HONG/article/details/81227598
今日推荐