POJ 1338 Ugly Numbers 数学题

       又是一道一模一样的题,有木有!以后碰到这样的题,瞬秒,能不能!!题目:

Ugly Numbers
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 15356   Accepted: 6790

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
ac代码:

#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(){
	//freopen("4.txt","r",stdin);
  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++;
	//i++;
  }
  int n;
  while(scanf("%d",&n)&&n){
	printf("%lld\n",num[n]);
  }
  return 0;
}



你可能感兴趣的:(POJ 1338 Ugly Numbers 数学题)