본문 바로가기
알고리즘

[Java]프로그래머스 카펫

by irerin07 2019. 4. 15.
728x90

```

class Solution {
    public int[] solution(int brown, int red) {
        int[] answer = new int[2];
        int sum = brown + red;
        for(int i = 3; i <= sum; i++){
            if(sum % i == 0){
                int temp = sum / i;
                if(((temp-2)*(i-2)) == red){
                    answer[0] = temp;
                    answer[1] = i;
                    return answer;
                }
            }
        }
        return answer;
    }
}

```

시간은 좀 걸렸지만 규칙을 발견하니 생각보다 쉽게 푼 문제.

728x90

'알고리즘' 카테고리의 다른 글

[백준] 동전 0 [Java/자바]  (0) 2019.06.15
[Java]프로그래머스 더 맵게  (0) 2019.04.24
[Java]프로그래머스 위장  (0) 2019.04.15
[Java]프로그래머스 쇠막대기  (0) 2019.04.15
[Java]프로그래머스 프린터  (0) 2019.04.15