喵叔 发表于 2018-6-28 16:42:05

[求助]蛇行矩阵

创建蛇行矩阵 起始位置是左上角,然后依次顺时针增减
def main(n):
        tot=1
        a=[ for i in range(n)]
        x=0
        y=n-1
        # a=tot
        print(a)
        while(tot<n*n+1):
                while(x+1<n)and(not(a)):
                        a=tot
                        x+=1
                        tot+=1
                while(y-1>=0)and(not(a)):
                        a=tot
                        y-=1
                        tot+=1
                while(x-1>=0)and(not(a)):
                        a=tot
                        x-=1
                        tot+=1
                while(y+1<n)and(not(a)):
                        a=tot
                        y+=1
                        tot+=1
       
        print(a)
        # a=1
        # tot=a[]

main(3)
想问一下为什么最后一位总是为零,难道是没有赋值附上去吗

凌九霄 发表于 2018-6-28 18:43:16

直接举例说出你的要求

喵叔 发表于 2018-6-29 13:06:00

凌九霄 发表于 2018-6-28 18:43
直接举例说出你的要求

你跑下我的程序就知道了
页: [1]
查看完整版本: [求助]蛇行矩阵