1338 Ugly Numbers

来源:互联网 发布:nginx书籍 编辑:程序博客网 时间:2024/06/03 14:35

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

1290

Sample Output

1210

DISCUSS:

#include <stdio.h>inline long min(long a,long b){return a>b?b:a;}int main(int argc, char *argv[])  {  long buf[1500]={0},t2,t3,t5,num;int i=0,i2=0,i3=0,i5=0;buf[0]=1;for(i=1;i<1500;++i){t2=buf[i2]*2;t3=buf[i3]*3;t5=buf[i5]*5;buf[i]=min(min(t2,t3),t5);if(t2==buf[i])++i2;if(t3==buf[i])++i3;if(t5==buf[i]) ++i5;}while(scanf("%d",&num)!=EOF&&num)printf("%d\n",buf[num-1]);return 0;}