POJ1338 Ugly Numbers

来源:互联网 发布:快递单号记录软件 编辑:程序博客网 时间:2024/06/02 13:22

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


将2,3,5进行排列组合树1到1500之间的各个数(y=(2^a)*(3^b)*(5^c))

AC代码:

#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;int main(){int n;while(~scanf("%d",&n)&&n){int a[1501],i,p2=1,p3=1,p5=1,x;a[1]=1;for(i=2;i<=1500;i++){x=min(min(a[p2]*2,a[p3]*3),a[p5]*5);a[i]=x;if(x==a[p2]*2) p2++;if(x==a[p3]*3) p3++;if(x==a[p5]*5) p5++;}printf("%d\n",a[n]);}return 0;}



0 0
原创粉丝点击