Ugly Numbers(丑数)




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.

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 <string.h>#include <cstdio>#include <algorithm>#include <string>using namespace std;long long min(long long a,long long b,long long c){ long long x=a<b?a:b; return x<c?x:c;}int main(){ long long num[1505]; long long p2,p3,p5; p2=p3=p5=1; memset(num,0,sizeof(num)); num[1]=1; int i=1; while(i<=1501){ num[++i]=min(num[p2]*2,num[p3]*3,num[p5]*5);if(num[i]==num[p2]*2) p2++;if(num[i]==num[p3]*3) p3++;if(num[i]==num[p5]*5) p5++; } int n; while(scanf("%d",&n)&&n){printf("%lld\n",num[n]); } return 0;}
posted @ 2014-07-14 15:51  冷夏的博客园  阅读(398)  评论(0编辑  收藏  举报