d693: 最小公倍數

題目:http://zerojudge.tw/ShowProblem?problemid=d693
程式碼:
import java.util.Scanner;
public class d693 {

 public static void main(String[] args) {
  // TODO Auto-generated method stub
  Scanner sc = new Scanner(System.in);
  while(sc.hasNext()){
   double num,answer=1;
   int N = sc.nextInt();//有n個數字
   if(N==0) break;
   for(int i=0;i<N;i++){
    num = sc.nextInt();
    answer=(answer*num/gcd(num,answer));//最小公倍數1個1個做 
   }
   System.out.println((int)answer);
  }

 }
 public static double gcd(double m, double n) {//GCD
        return m%n == 0 ? n : gcd(n, m % n);
    }
 

}

留言

熱門文章