https://programmers.co.kr/learn/courses/30/lessons/42842?language=java
완전탐색 - 카펫
해결 방법
메인 아이디어
- 노란색 격자의 수로 가로가 세로보다 큰 중앙 사각형을 만든다
- 중앙 사각형을 두르고 있는 테두리에 사용되는 갈색 격자의 갯수를 구한다
- 입력된 갈색 격자의 갯수와 같을 경우 테두리를 포함한 사각형의 크기를 리턴
- 노란색 격자의 수를 1부터 제곱근까지의 수로 나누어서 나뉘어질 경우 사각형 생성 가능
( 나눈 숫자가 세로에 사용된 노란색 격자의 수 몫이 가로에 사용된 수 )
- 그 때 테두리에 가용된 갈색 격자의 수는
( 가로의 길이 + 2 ) * 2 + 세로의 길이 * 2
- 갈색 격자의 수가 입력된 갈색 격자의 수와 같으면 테두리가 포함된 사각형의 크기를 리턴
최종 코드
import java.util.Arrays; // https://programmers.co.kr/learn/courses/30/lessons/42842?language=java // 완전탐색 - 카펫 public class Programmers_42842 { public static void main(String[] args) { Solution s = new Solution(); System.out.println(Arrays.toString(s.solution(10,2))); System.out.println(Arrays.toString(s.solution(8,1))); System.out.println(Arrays.toString(s.solution(24,24))); } } class Solution{ int width, height; int [] answer = new int[2]; int [] solution(int brown, int yellow){ for( int i=1;i<=Math.sqrt(yellow);i++){ if( yellow % i == 0){ width = (int)(yellow / i); height = i; if( (width + 2) * 2 + 2 * height == brown) { answer[0] = width + 2; answer[1] = height + 2; } } } return answer; } }