Skip to content

Instantly share code, notes, and snippets.

@kFTY
Created May 12, 2017 15:25
Show Gist options
  • Save kFTY/0b715fe449c526cb87e7ad9cd32b04b7 to your computer and use it in GitHub Desktop.
Save kFTY/0b715fe449c526cb87e7ad9cd32b04b7 to your computer and use it in GitHub Desktop.
交作业:生成杨辉三角形
def yanghui(m, n):
# preprocess m, n
m = int(m)
n = int(n)
# validate n
if n > m or m < 0 or n < 0:
return "Invalid query"
# first and last
if n == 0 or n == m:
return 1
return yanghui(m - 1, n - 1) + yanghui(m - 1, n)
def generate_yh(m):
for row in range(m + 1):
for col in range(row + 1):
print(yanghui(row, col), end="\t")
print ("\n")
generate_yh(5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment