Created
May 12, 2017 15:25
-
-
Save kFTY/0b715fe449c526cb87e7ad9cd32b04b7 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
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