首页 > 解决方案 > 嗨,我是编程新手,如何执行此代码?

问题描述

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)

标签: matrix

解决方案


我稍微修改了你的程序。

现在,要运行这个程序,这里这里列出了一些步骤。

#!/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)


推荐阅读