题解 | #蛇形矩阵#
蛇形矩阵
http://www.nowcoder.com/practice/649b210ef44446e3b1cd1be6fa4cab5e
找规律while True: try: N = int(input()) row_1 = 1 for i in range(N): if i == 0: row_s = 1 else: row_s = sum(range(0,i+1))+1 for j in range(N-i): if i == 0 and j == 0: column = 1 print(column, end = ' ') elif j == 0 : print(row_s, end= ' ') else: column = row_s + sum(range(i+2,i+j+2)) print(column, end = ' ') print() except: break整理边界条件,看看是否可以包含于公式之中,发现所有边界条件在公式里能包含,故精简:
while True:
try:
N = int(input())
for i in range(N):
row_s = sum(range(0,i+1))+1
for j in range(N-i):
column = row_s + sum(range(i+2,i+j+2))
print(column, end = ' ')
print()
except:
break