📜  形成 chakravyuh 矩阵 python 代码示例

📅  最后修改于: 2022-03-11 14:46:18.108000             🧑  作者: Mango

代码示例1
num=int(input("enter the length of square matrix"))
n_list=[[0 for x in range(num)] for y in range(num)]
n=1
low=0
high=num - 1
count=int((num+1)/2)

for i in range(count):
    for j in range(low,high+1):
        n_list[i][j]=n
        n+=1

    for j in range(low+1,high+1):
        n_list[j][high]=n
        n+=1

    for j in range(high-1,low-1,-1):
        n_list[high][j]=n
        n+=1

    for j in range(high-1,low,-1):
        n_list[j][low]=n
        n+=1

    low+=1
    high-=1



for i in range(num):
    for j in range(num):
        print(n_list[i][j],end="\t")
    print()