Ugly Numbers
题意描述:丑数是因子只有2,3,5的数,编写一个程序输出第1500个丑数
解题思路:利用set,先把数1存进去,因为1也是丑数,然后再定义一个数存丑数1,接着把前面存入set里的数的2、3、5倍的数存入set,因为set里面的数有序且不同,所以我们只需要再用一个数记录存了多少个数,当存到1500时输出即可
AC:
#include <iostream>
#include <set>
typedef long long ll;
using namespace std;
int main()
{
set<ll> num; //有序且不同
num.insert(1);
int ans=0;
set<ll>::iterator it;
for(it=num.begin();it!=num.end();it++)
{
ans++;
if(ans==1500) printf("The 1500'th ugly number is %lld.\n",*it);
ll t=*it;
num.insert(*it*2);
num.insert(*it*3);
num.insert(*it*5);
}
return 0;
}