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 and Output
There is no input to this program. Output should consist of a single line as shown below, with <number> replaced by the number computed.
Sample output
The 1500'th ugly number is <number>.
#include<iostream>
#include<cstdio>
using namespace std;
int ugly[1506];
int getmin(int a,int b,int c)//找出三个数中最小的
{
if(a<=b)
{
if(a<=c) return a;
else return c;
}
else
{
if(b<=c) return b;
else return c;
}
}
int find_ugly(int n)
{
int i2=1,i3=1,i5=1;//设定三个游标因子
ugly[1]=1;
int i=2;
int min;
while(i<=n)
{
min=getmin(ugly[i2]*2,ugly[i3]*3,ugly[i5]*5);
ugly[i]=min;
//如果因子被用过,那么游标就加一,确保不重不漏
if(min==ugly[i2]*2) i2++;
if(min==ugly[i3]*3) i3++;
if(min==ugly[i5]*5) i5++;
i++;
}
return ugly[n];
}
int main()
{
int n;
while(cin>>n)
{
cout<<find_ugly(n)<<endl;
}
return 0;
}