設計一個正方形矩陣類,實作輸出斜行方陣,
樣例輸入
5
樣例輸出
11 7 4 2 1
16 12 8 5 3
20 17 13 9 6
23 21 18 14 10
25 24 22 19 15
uj5u.com熱心網友回復:
def FormMatrix(num):
curNum,maxNum = 1,num**2
lstData = [[] for i in range(num)]
f_initCol,f_initRow,f_curRow,f_curCol=num -1,0,0,num -1
while curNum <= maxNum:
lstData[f_curRow].append(curNum)
f_curRow +=1
f_curCol +=1
if f_curRow <num and f_curCol == num:
f_initCol -=1
f_curCol = f_initCol
f_curRow = f_initRow = 0
elif f_curRow ==num and f_curCol == num:
f_curCol = f_initCol =0
f_curRow = f_initRow = 1
elif f_curRow == num and f_curCol< num:
f_curCol = f_initCol =0
f_initRow +=1
f_curRow = f_initRow
curNum +=1
return lstData
l = FormMatrix(5)
print([i[::-1] for i in l])
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/82704.html
上一篇:Python問題討論
