First, solve the problem. Then, write the code. ” - John Johnson

It's not at all important to get it right the first time. It's vitally important to get it right the last time. ” - Andrew Hunt and David Thomas

Wednesday, July 16, 2014

Ugly Numbers

Author : Jisan
Programming language : Java
Difficulty : Easy
Status : Accepted
Problem number : UVA-136
http://uva.onlinejudge.org/index.php?option=onlinejudge&page=show_problem&problem=72

Help:http://docs.oracle.com/javase/7/docs/api/java/lang/Math.html

Code:

import java.util.*;
public class Uva_136 {
   public static void main(String args[]){
  int[] tomato=new int[1501];
  int[] A={1,1,1};
  int[] factors={2,3,5};
  tomato[1]=1;
  for(int i=2;i<=1500;i++){
  tomato[i]=Math.min(2*tomato[A[0]], Math.min(3*tomato[A[1]], 5*tomato[A[2]]));
  for(int j=0;j<A.length;j++){
  if(factors[j]*tomato[A[j]]==tomato[i]){
  A[j]++;
  }
  }
  }
  System.out.format("The 1500'th ugly number is %d.\n",tomato[1500]);
   }
}

No comments:

Post a Comment