본문 바로가기
Algorithm

[프로그래머스] 카펫 파이썬

by YGSEO 2021. 3. 12.
728x90

완전탐색으로 푸는 방법

def solution(brown, red):
    for a in range(1, int(red**0.5)+1):
        if not red % a:
            b = red // a
            if 2*a + 2*b + 4 == brown:
                 return [b+2, a+2]

출처:

geonlee.tistory.com/114

def solution(brown, red):
    for index in range(1,red+1):
        if red%index == 0: 
            length = red//index
            if (((index+2)*(length+2))-(index*length)) == brown:
                return [max(index+2,length+2),min(index+2,length+2)]

m.blog.naver.com/PostView.nhn?blogId=jaeyoon_95&logNo=221742960068&proxyReferer=https:%2F%2Fwww.google.com%2F


방정식으로 푸는 방법

def solution(brown, red):
  x = (brown + 4 + ((brown + 4)**2 - 16*(brown+red))**0.5)/4
  y = (brown + red) // x
  return [max(x,y), min(x,y)]

 

출처:

leedakyeong.tistory.com/entry/%ED%94%84%EB%A1%9C%EA%B7%B8%EB%9E%98%EB%A8%B8%EC%8A%A4-%EC%B9%B4%ED%8E%AB-in-python

728x90

댓글