~cpp /* * Created on 2005. 3. 30 */ /** * @author 중앙대 컴퓨터공학과 01 이동현 */ import java.util.*; import java.math.*; public class UglyNumbers { public ArrayList arr; /** * n을 arr에 삽입하되 중복값이 있으면 아무것도 하지않아. * @param n double 삽입할 값 * @return int 코드 1:삽입완료 -1:이미 값 있음 1:맨 마지막에 추가 */ public int insert(double n) { for (int i = 0; i < arr.size(); i++) { if (((Double) arr.get(i)).doubleValue() > n) { arr.add(i, new Double(n)); return 1; } else if (((Double) arr.get(i)).doubleValue() == n) return -1; } arr.add(new Double(n)); return 0; } public int start() { int index = 1; arr = new ArrayList(); arr.add(new Double(1.0)); while (index != 1500) { insert(((Double) arr.get(0)).doubleValue() * 2.0); insert(((Double) arr.get(0)).doubleValue() * 3.0); insert(((Double) arr.get(0)).doubleValue() * 5.0); arr.remove(0); index++; } System.out.println("The 1500'th ugly number is "+new BigDecimal(((Double)arr.get(0)).doubleValue()));// + " " + arr.size()); return 0; } public static void main(String[] args) { UglyNumbers ug = new UglyNumbers(); ug.start(); } }