各位大佬为什么n是奇数时候中间数字是0
查看原帖
各位大佬为什么n是奇数时候中间数字是0
1552051
Rleave楼主2025/1/1 15:00
num = 1
top = 0
bottom = n
left = 0
right = n
if n == 1:
    print(1)
else:
    for i in range(n*n):
        for j in range(left,right-1): #填充第一行3
            s[top][j] = num
            num += 1
        top += 1 # 第一行填完了 缩小一行
        for j in range(top-1,bottom-1): # 填充最右列3
            s[j][right-1] = num
            num += 1
        right -= 1 # 最右列填完 缩小
        if top < bottom:
            for j in range(right,left,-1): # 填充最下行3
                s[bottom-1][j] = num
                num += 1
            bottom -= 1 #最下行填完 缩小
        if left < right:
            for j in range(bottom,top-1,-1): # 填充最左列3
                s[j][left] = num
                num += 1
            left += 1 # 最左列填完
2025/1/1 15:00
加载中...