首页 > 专题系列 > Java解POJ > POJ 1338 Ugly Numbers [解题报告] Java
2013
11-09

POJ 1338 Ugly Numbers [解题报告] Java

Ugly Numbers

问题描述 :

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.

输入:

Each line of the input contains a postisive integer n (n <= 1500).Input is terminated by a line with n=0.

输出:

For each line, output the n’th ugly number .:Don’t deal with the line with n=0.

样例输入:

1
2
9
0

样例输出:

1
2
10

解题代码:

//* @author  [email protected]
import java.util.Scanner;
import java.util.Vector;
public class Main {
    public static void main(String[] args) {
        Scanner scan=new Scanner(System.in);
        Vector< Long> v=new Vector< Long>();
        v.add(new Long(1));
        int i=0,j=0,k=0;
        while(v.size()< 1500){
            v.add(Math.min(Math.min(v.get(i)*2, v.get(j)*3), v.get(k)*5));
           if(v.get(v.size()-1).equals(v.get(v.size()-2))){v.remove(v.size()-1);}
            if(v.get(i)*2==v.get(v.size()-1))i++;
            else if(v.get(j)*3==v.get(v.size()-1))j++;
            else k++;
        }
       int n=1;      
       while((n=scan.nextInt())!=0){
            System.out.println(v.get(n-1));
        }        
    }
}

  1. 站长好。我是一个准备创业的互联网小白,我们打算做一个有关国*际*游*学的平台。手上也有了一些境外资源。现阶段的团队现在没有cto.原意出让一些管理股寻找一个靠谱的技术专家做合伙人, 不知道是不是能得到您的帮助。发个帖子或者其他方式。期待您的回应。可以加我微信tianxielemon聊聊。