题解 | #蛇形矩阵#
蛇形矩阵
http://www.nowcoder.com/practice/649b210ef44446e3b1cd1be6fa4cab5e
#n=int(4)
a=[]
for i in range(n):
for j in range(i+1):
a.append((i-j,j))
s=[[0 for i in range(n)] for i in range(n)]
all=int(0.5*n*(n+1))
for j in range(all):
s[a[j][0]][a[j][1]]=str(j+1)
for k in range(n):
for l in range(n):
if s[k][l]!=0:
print(int(s[k][l]), end=(' '))
print('')
#首先确定数列的位置坐标:(0,0),(1,0),(0,1),(2,0),(1,1),(0,2);然后打算用numpy创建array,然后赋值,再打印。但是numpy不让用,只能这样了 s=[[0 for i in range(n)] for i in range(n)]