멀쩡한 사각형

Updated:

GCD문제이겠거니 눈치는 챘는데, 정확한 식 도출에는 실패하였다.

설명은 여기가 아주 잘되어있다. 수학 문제에 상당히 가까운 문제이다

https://leedakyeong.tistory.com/entry/프로그래머스-멀쩡한-사각형-in-python

by Java

class Solution {
	public long solution(int w,int h) {
		long answer = (long)w*h;
        int res = gcd(w,h);
		return answer - res*((w/res)+(h/res)-1);
	}
    private int gcd(int a,int b) {
        if(a%b==0) return b;
        return gcd(b%a,a);
    }
}

Leave a comment