题解 | #超级圣诞树# 递归
超级圣诞树
http://www.nowcoder.com/practice/470d26c9a73e4e17be8cc45cac843423
先按规律处理好,放进ans数组,再把右侧多余的空格干掉。
n = int(input())
def copy(ans,tmp,sx,sy):
for i in range(sx,sx+len(tmp)):
ans[i][sy:sy+len(tmp[0])] = tmp[i-sx][:]
def solve(dep):
if dep == 1:
return [
[0,0,1,0,0],
[0,1,0,1,0],
[1,0,1,0,1],
]
tmp = solve(dep-1)
ans = [[0] * (2*len(tmp[0])+1) for i in range(2*len(tmp))]
copy(ans,tmp,0,1 + (len(tmp[0]) >> 1))
copy(ans,tmp,len(tmp),0)
copy(ans,tmp,len(tmp),1 + len(tmp[0]))
return ans
ans = solve(n)
for i,line in enumerate(ans):
line.reverse()
line = line[line.index(1):]
line.reverse()
ans[i] = ''.join([' ' if not v else '*' for v in line])
ans.extend([' ' * (len(ans[-1])>>1) + '*'] * n)
for line in ans: print(line)