선릉역 1번 출구
baekjoon - 재귀 본문
1. 10872
def factorial(num): if num < 2: return 1 return num * factorial(num-1) a = int(input()) print(factorial(a)) |
n = int(input()) def fac(n): if n == 0: return 1 elif n == 1: return 1 return n*fac(n-1) print(fac(n)) |
2. 10870
n = int(input()) def fibo(n): if n == 1: return 0 elif n == 2: return 1 return fibo(n-1)+fibo(n-2) print(fibo(n+1)) |
3. 2447 - 도형 문제
def square(x,y,n,matrix): if n == 3: matrix[x][y] = "*" matrix[x][y+1] = "*" matrix[x][y+2] = "*" matrix[x+1][y] = "*" matrix[x+1][y+2] = "*" matrix[x+2][y] = "*" matrix[x+2][y+1] = "*" matrix[x+2][y+2] = "*" return square(x,y,n//3,matrix) square(x,y+n//3,n//3,matrix) square(x,y+n//3*2,n//3,matrix) square(x+n//3,y,n//3,matrix) square(x+n//3,y+n//3*2,n//3,matrix) square(x+n//3*2,y,n//3,matrix) square(x+n//3*2,y+n//3,n//3,matrix) square(x+n//3*2,y+n//3*2,n//3,matrix) matrix = [[" "]*2201 for i in range(2201)] num = int(input()) square(0,0,num,matrix) for i in range(num): string = '' for j in range(num): string += matrix[i][j] print(string) |
def square(x,y,n,matrix): if n == 1: matrix[x][y] = "*" return //1일때 출력 후 return square(x,y,n//3,matrix) square(x,y+n//3,n//3,matrix) square(x,y+n//3*2,n//3,matrix) square(x+n//3,y,n//3,matrix) square(x+n//3,y+n//3*2,n//3,matrix) square(x+n//3*2,y,n//3,matrix) square(x+n//3*2,y+n//3,n//3,matrix) square(x+n//3*2,y+n//3*2,n//3,matrix) //다음 좌표를 재귀로 호출 matrix = [[" "]*2201 for i in range(2201)] num = int(input()) square(0,0,num,matrix) for i in range(num): string = '' for j in range(num): string += matrix[i][j] print(string) |
4. 11729 - hanoi
def hanoi(n, fro, mid, to): if n == 1: print(fro, to) return hanoi(n-1, fro, to, mid) print(fro, to) hanoi(n-1, mid, fro, to) num = int(input()) sum = 1 for i in range(num - 1): sum = sum * 2 + 1 print(sum) hanoi(num,1,2,3) |
'Algorithm > Algorithm 문제풀이' 카테고리의 다른 글
baekjoon - 9093 (0) | 2021.08.28 |
---|---|
baekjoon - 10828 (0) | 2021.08.28 |
baekjoon - 브루트 포스 (0) | 2021.08.19 |
baekjoon - 기본 수학2 (0) | 2021.08.18 |
baekjoon - 기본 수학1 (0) | 2021.08.17 |
Comments