본문 바로가기
알고리즘 & 자료구조/프로그래머스

카펫

by 신재권 2023. 1. 2.
package programmers;

import java.util.Arrays;

public class 카펫 {

   public static int[] solution(int brown, int yellow) {
      int sum = brown + yellow;

      return find(yellow, sum);
   }

   private static int[] find(int yellow, int sum) {
      int y = 0, x = 0;

      for (int i = 1; i <= yellow; i++) {
         if (yellow % i == 0) {
            y = Math.min(i, yellow / i);
            x = Math.max(i, yellow / i);
            if ((y + 2) * (x + 2) == sum) {
               break;
            }
         }
      }

      return new int[] {x + 2, y + 2};
   }

   public static void main(String[] args) {
      System.out.println(Arrays.toString(solution(10, 2)));
      System.out.println(Arrays.toString(solution(8, 1)));
      System.out.println(Arrays.toString(solution(24, 24)));
   }
}

'알고리즘 & 자료구조 > 프로그래머스' 카테고리의 다른 글

영어 끝말잇기  (0) 2023.01.04
짝지어 제거하기  (0) 2023.01.03
다음 큰 숫자  (0) 2023.01.01
피보나치 수  (0) 2022.12.31
숫자의 표현  (0) 2022.12.30