#10798: java 測試OK,解題時 RE


a1018345 (飄邈仙人)

學校 : 不指定學校
編號 : 56982
來源 : [114.42.79.104]
最後登入時間 :
2016-04-10 17:14:20
a024. 最大公因數(GCD) | From: [114.36.103.25] | 發表日期 : 2016-03-19 22:38

package tw.LanYao.myproject.HomeWork;

import java.util.ArrayList;
import java.util.Scanner;

public class 查詢2數最大公因數 {

 public static void main(String[] args) {
  // TODO Auto-generated method stub

  Scanner sca1 = new Scanner(System.in);

  while (sca1.hasNext()) {
   int a = sca1.nextInt();
   int b = sca1.nextInt();

   ArrayList aa1 = new ArrayList();
   ArrayList aa2 = new ArrayList();
   int n = 0;
   for (int i = 2; i <= a; i++) {
    if (a % i == 0) {
     aa1.add(i);
     n++;
    }
   }
   n = 0;
   for (int i = 2; i <= b; i++) {
    if (b % i == 0) {
     aa2.add(i);
     n++;
    }
   }

   ArrayList ans = new ArrayList();
   for (int i = aa1.size(); i > 0; i--) {
    for (int n1 = aa2.size(); n1 > 0; n1--) {
     if (aa1.get(i - 1) == aa2.get(n1 - 1)) {
      ans.add(aa1.get(i - 1));

     }

    }
   }
   System.out.println(ans.get(0));
  }

 }

}

 
ZeroJudge Forum