poj 1338 Ugly Numbers

Ugly Numbers

Time Limit: 1000MS
Memory Limit: 10000K

Total Submissions: 15213
Accepted: 6714

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>
using namespace std;
int mini(int a,int b)
{
	if(a<=b)
		return a;
	return b;
}
int main()
{
	int i,j,k;
	int n;
	int m;
	int temp;
	int ugly[1501];
	ugly[0]=1;
	n=1;
	i=0;j=0;k=0;
    while(n<1501)
    {
    	temp=mini(mini(ugly[i]*2,ugly[j]*3),ugly[k]*5);
    	if(temp==ugly[i]*2)
    		i++;
   		if(temp==ugly[j]*3)
    		j++;
   		if(temp==ugly[k]*5)
    		k++;
   		ugly[n]=temp;
   		n++;
    }
	while(1)
	{
		cin>>m;
		if(m==0)
			break;
		else
			cout<<ugly[m-1]<<endl;
	}
	return 0;
}

 

体验用windows live writer发日志~

折腾了好半天。。。。

posted @ 2011-11-21 16:34  w0w0  阅读(130)  评论(0编辑  收藏  举报