Ugly Numbers
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 20880 | Accepted: 9268 |
Description
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, ...
shows the first 10 ugly numbers. By convention, 1 is included.
Given the integer n,write a program to find and print the n'th ugly number.
1, 2, 3, 4, 5, 6, 8, 9, 10, 12, ...
shows the first 10 ugly numbers. By convention, 1 is included.
Given the integer n,write a program to find and print the n'th ugly number.
Input
Each line of the input contains a postisive integer n (n <= 1500).Input is terminated by a line with n=0.
Output
For each line, output the n’th ugly number .:Don’t deal with the line with n=0.
Sample Input
1 2 9 0
Sample Output
1 2 10
#include<iostream> #include<algorithm> using namespace std; int a[1502]={0,1}; int main() { int num2=2,num3=3,num5=5; int i=1,j=1,k=1; int t=0,s; for(s=2;s<1501;++s) { t=min(num2,min(num3,num5)); a[s]=t; if(t==num2) num2=a[++i]*2; if(t==num3) num3=a[++j]*3; if(t==num5) num5=a[++k]*5; } while(cin>>s&&s) cout<<a[s]<<endl; return 0; }