matrix - 嗨,我是编程新手,如何执行此代码?
问题描述
def cubomagico (matriz,fil,col,c,n):
if(c==n*n):
matriz[n-1][col]=c
else:
if(fil<0 and col==n):
cubomagico(matriz, fil+2,n-1, c, n)
else:
if(fil<0):
cubomagico(matriz,n-1,col,c,n)
else:
if(col==n):
cubomagico(matriz,fil,0,c,n)
else:
if(matriz[fil][col]==0):
matriz[fil][col]=c
cubomagico(matriz, fil-1,col+1,c+1,n)
else:
cubomagico(matriz, fil+2,col-1,c,n)
解决方案
我稍微修改了你的程序。
现在,要运行这个程序,这里或这里列出了一些步骤。
#!/usr/bin/python3
def cubomagico(matriz,fil,col,c,n):
if (c==n*n):
matriz[n-1][col]=c
elif (fil<0 and col==n):
cubomagico(matriz, fil+2,n-1, c, n)
elif (fil<0):
cubomagico(matriz,n-1,col,c,n)
elif (col==n):
cubomagico(matriz,fil,0,c,n)
elif (matriz[fil][col]==0):
matriz[fil][col]=c
cubomagico(matriz, fil-1,col+1,c+1,n)
else:
cubomagico(matriz, fil+2,col-1,c,n)
C,N=4,4
M=[[0]*C for i in range(0,N)]
cubomagico(M,1,1,C,N)
print(M)
推荐阅读
- html - 定位待办事项列表元素的问题
- algorithm - 给定一个对象向量,查找范围最快的速度是多少?
- dart - 如何为大量 CheckboxListTiles 创建一个布尔值?
- python - 在 Python Web 驱动程序中检索 HTML 元素对象
- c++ - 图中的 MST 相关边
- api - JWT 令牌似乎是正确的,但收到的响应是 401
- python - 熊猫列中不同组的不同缩放比例
- java - 如何修复“com.mysql.cj.jdbc.exceptions.CommunicationsException”错误
- nativescript - 如何在本地 chrome 和本地模拟器中调试 nativescript
- java - 在向布局添加按钮时获取 java.lang.reflect.InvocationTargetException