题目
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 .
思路
题意很简单找第1500个由2,3,5未因数的数字
不确定大小可以先定义longlong
代码
#include"bits/stdc++.h"
using namespace std;
int main(){
set<long long>s;
s.insert (1);
int num=0;
set<long long>::iterator it;
for(it=s.begin ();it!=s.end ();it++){
num++;
if(num==1500){printf("The 1500'th ugly number is %lld.\n",*it);break;}
s.insert ((*it)*2);s.insert ((*it)*3);s.insert ((*it)*5);//把每个数都*2,*3,*5.因为set的特性会自动排序,找到第1500个数直接输出即可
}
}