-
Notifications
You must be signed in to change notification settings - Fork 0
/
Level10_recursion.py
70 lines (58 loc) · 1.21 KB
/
Level10_recursion.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# Step 1: No.10872
def factorial(n):
if n > 0:
num = factorial(n-1)*n
else:
num = 1
return num
N = int(input())
print(factorial(N))
# Step 2: No.10870
def Fibonacci(n):
if n >= 2:
ans = Fibonacci(n-1) + Fibonacci(n-2)
elif n == 1:
ans = 1
else:
ans = 0
return ans
N = int(input())
print(Fibonacci(N))
# Step 3: No.2447
def printstar(n):
global P
if n == 3:
P[0][:3] = P[2][:3] = [1]*3
P[1][:3] = [1,0,1]
return
printstar(n//3)
for i in range(3):
for j in range(3):
if i==1 and j==1:
continue
for k in range(n//3):
P[(n//3)*i+k][(n//3)*j:(n//3)*(j+1)] = P[k][:n//3]
N = int(input()) # N=3^k (k=1~8)
P = [[0 for i in range(N)] for i in range(N)]
printstar(N)
for i in P:
for j in i:
if j:
print('*',end='')
else:
print(' ',end='')
print()
# Step 4: No.11729
def hanoi(n,a,c,b):
if n == 1:
path.append([a,c])
else:
hanoi(n-1,a,b,c)
hanoi(1,a,c,b)
hanoi(n-1,b,c,a)
N = int(input())
path = []
hanoi(N,1,3,2)
print(len(path))
for i in path:
print(i[0],i[1])