用python打印下列矩阵
发布网友
发布时间:2022-04-26 15:21
我来回答
共1个回答
热心网友
时间:2023-01-23 08:19
按照你的要求编写的Python语言的矩阵程序如下
#!/usr/bin/python
# -*- coding: UTF-8 -*-
def printMatrix(n):
arr = [ [0 for i in range(n)] for j in range(n)]
curNum = 1
for i in range(0,2*n-1):
if i%2 == 0:
for j in range(0,n):
for k in range(0,n):
if j+k==i:
arr[j][k] = curNum
curNum=curNum+1
else:
for j in range(n-1,-1,-1):
for k in range(n-1,-1,-1):
if j+k==i:
arr[j][k] = curNum
curNum=curNum+1
for i in range(0,n):
for j in range(0,n):
print (arr[i][j],"\t",end='')
print ("\n")
printMatrix(10)