[프로그래머스] 완전탐색 - 카펫(Python3)

kimgaeul02·2023년 2월 2일

프로그래머스

목록 보기
8/11

📄 카펫

🔗 완전탐색 : 카펫


📌 해석

전체 면적 : total_grid = brown + yellow = width x height
노란색 면적 : yellow = (width-2) x (height-2)
갈색 면적 : brown = total_grid - yellow

검산 식은 연립방정식을 통하여 구한다.
brown = total_grid - yellow,
brown = total_grid - (width x height) +2(width+height) -4,
brown = 2(width+height) -4

width와 height를 구한 뒤 brown = 2(width+height) -4가 참인 경우 출력한다.

📌 풀이

전체 카펫 면적을 구한다. 이후 for문을 이용해 카펫 세로 길이를 증가시키며 가로, 세로 길이를 구한다. 이때 가로는 세로보다 크거나 같기 때문에 if height <= width: 조건문을 집어넣었다. 이후 상단에서 구한 연립방정식 brown = 2(width+height) -4을 이용해 검산한다. 구한 가로, 세로 값이 참이라면 결과를 출력한다.

💻 코드

def solution(brown, yellow):
    answer = []
    total = brown + yellow
    
    for width in range(1,total+1):
        if (total / width) %1 == 0:
            height = total / width
            if height <= width:
                if brown == width*2 + height*2 -4:
                    return [width, height]
    
    return answer

0개의 댓글