abbc0209   2달 전

제가 부족해서 그런지 어디에서 문제인지 잘 모르겠네요...

약수를 저장했다가 나중에 써먹야 하는건가요?


import java.util.ArrayList;
import java.util.Scanner;
 
public class Main { //1037
public static int commmonNum;
    public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    int testCase = scanner.nextInt(); //insert number of case
    //ArrayList<Integer> list = new ArrayList<>();
    int commonMultiple =1;
    int maxNum=0;
    for(int i=1; i<=testCase; i++){
    int tmp = scanner.nextInt();
    if(tmp>maxNum) maxNum = tmp;
    if(commonMultiple > tmp) commonMultiple=calculatingCommonMultiple(commonMultiple, tmp);
    else commonMultiple=calculatingCommonMultiple(tmp, commonMultiple);
    }
   
    if(maxNum>=commonMultiple){
    if(commmonNum==1)commmonNum = commonMultiple; // not needed code...
    commonMultiple*=commmonNum;
    } //not efficient
   
    System.out.println(commonMultiple); //result
    }
    public static int calculatingCommonMultiple(int x, int y){
    commmonNum = calculatingCommonDivisor( x,  y);
    return x*y/commmonNum; //commmonNum*(x/commmonNum)*(y/commmonNum)
    }
    public static int calculatingCommonDivisor(int x, int y){
    if(y==0) return x;
    return calculatingCommonDivisor(y, x%y);
    }
}


댓글을 작성하려면 로그인해야 합니다.