Last active
June 10, 2020 08:32
-
-
Save DongguemYoo/98bd7f2dc0d68976d25985717f0d069c to your computer and use it in GitHub Desktop.
농장 나누기(재귀함수)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//크기가 n*x인 농장을 정사각형으로 나누고 나온 정사각형을 개수를 리턴하는 함수 | |
def solution(n): | |
answer = 0 | |
answer = divine(n[0],n[1],answer) | |
return answer | |
///big : 길이가 큰 면 | |
///smal: 길이가 작은 면 | |
///answer : 나온 정사각형을 저장할 변수 | |
def divine(big,small,answer): | |
if small == 0: //더 이상 나눌수 없는 상황이면 | |
return answer //이때까지 나온 정사각형을 리턴한다 | |
lastmapSize= big-int(big /small)*small // 정사각형을 만들고 남은 변의 길이를 저장한다 | |
answer+=int(big /small) //자른 정사각형의 개수를 더해준다 | |
return divine(small,lastmapSize,answer) // small이 big이 되고 남은 길이가 small이 된다 | |
n =[1680, 640] | |
print(solution(n)) | |
///실제 정답은 정확하게 전부다 같은 크기로 자를수있는 최소의 정사각형을 구하는 문제이다 그러므로 이런식으로 변경해야한다 | |
def solution(n): | |
answer = 0 | |
divineValue =divine(n[0],n[1],answer) //더이상 나누어지지 않는 divineValue를 구한뒤 | |
//해당 divine 발류로 처음 주어진 사각형을 자른다 | |
answer = int(n[0]/divineValue) * int(n[1]/divineValue) | |
return answer | |
def divine(big,small,answer): | |
if small == 0: | |
return answer | |
lastmapSize= big-int(big /small)*small | |
if lastmapSize == 0: | |
return small; ///더이상 나누어 지지않는 divinevalue 를 리턴하여서 | |
answer+=int(big /small) | |
return divine(small,lastmapSize,answer) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment