JAVA/JAVA 연습

JAVA 토끼와 거북이 (최대 공약수 ,최소공배수)

HicKee 2022. 9. 30. 15:58

토끼는 N분에 한 번 휴식을 하고 거북이는 M분에 한 번 휴식을 함

토끼와 거북이가 처음으로 같이 쉬는 시간은 언제?

 

입력

3 5

출력

15

code

import java.util.*;

public class Main {
    
	public static void main(String args[]) {
        int rabbit, turtle;
        Scanner scan = new Scanner(System.in);
        
        rabbit = scan.nextInt();
        turtle = scan.nextInt();
        
        int a,b,r;
        a = rabbit;
        b = turtle;
        r = 1;

       boolean c = true;
        //최대 공약수
        while(c){
            if(b%a !=0){

                r=b%a;
                b=a;
                a=r;
                c=true;
               
            }
            else{
                r= a;
                c = false;
            } 
        }
        //최소 공배수 공식 (a*b)/최대 공약수
        System.out.println((rabbit*turtle)/r);    
	}
}