Ai
1 Star 0 Fork 1

jack2583/PythonExamples

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
spiralmatrix.py 733 Bytes
一键复制 编辑 原始数据 按行查看 历史
Jérôme Krell 提交于 2019-10-10 20:22 +08:00 . Reformat Code by PyCharm-Community
n = int(input("Enter the size of matrix:"))
t = 1
r = 0 # r stands for row
c = 0 # c stands for column
matrix = [[0 for x in range(n)] for y in range(n)] # to initialise the matrix
if n % 2 == 0:
k = n // 2
else:
k = int((n / 2) + 1)
for i in range(k):
while c < n:
matrix[r][c] = t
t = t + 1
c = c + 1
r = r + 1
c = c - 1
while r < n:
matrix[r][c] = t
t = t + 1
r = r + 1
r = r - 1
c = c - 1
while c >= i:
matrix[r][c] = t
c = c - 1
t = t + 1
c = c + 1
r = r - 1
while r > i:
matrix[r][c] = t
t = t + 1
r = r - 1
r = r + 1
n = n - 1
c = c + 1
for m in matrix:
print(m)
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/jack2583/pythonExamples.git
git@gitee.com:jack2583/pythonExamples.git
jack2583
pythonExamples
PythonExamples
master

搜索帮助