poj-1338-Ugly Numbers

来源:互联网 发布:淘宝cpu散片哪家好 编辑:程序博客网 时间:2024/05/20 20:21
Ugly Numbers
Time Limit: 1000MS Memory Limit: 10000KTotal 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. 

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
#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;}


0 0
原创粉丝点击