문제
![](https://velog.velcdn.com/images/sheltonwon/post/d3159393-5931-4919-a061-12cd26d05c81/image.png)
문제링크
접근
![](https://velog.velcdn.com/images/sheltonwon/post/226fa355-9240-441b-8825-1015f8a271a0/image.png)
- 그림을 계속 보다가 저런 모양이 계속 반복되는 것을 보고 최대공약수를 생각해내었다.
- 자료형 유의하기
소스 코드
class Main {
public static void main(String[] args) throws Exception {
int w = 8;
int h = 12;
Solution sol = new Solution();
System.out.println("result : " + sol.solution(w, h));
}
}
class Solution {
public long solution(int w, int h) {
long answer = 1;
int maxDiv = 1;
int i = 1;
while(i <= w && i <= h) {
if(w % i == 0 && h % i == 0) maxDiv = i;
i++;
}
answer = (long) w * (long) h - (w + h);
answer += maxDiv;
return answer;
}
}