用python编写一个程序来检查给定的矩阵是否形成幻方

ckocjqey  于 2023-01-03  发布在  Python
关注(0)|答案(1)|浏览(233)

一个篮子是以矩阵的形式提供给你的。如果矩阵的大小是N x N,那么你可以把鸡蛋的数量范围是1到N2。你的任务是安排篮子里的鸡蛋,使每行,列和矩阵对角线的总和保持不变。测试案例供你参考:由用户输入篮子的尺寸,即N = 3,因此,可以放入每个槽的鸡蛋数量在1到32(1到9)的范围内。输入:6 3 6 5 5 5 4 7 4
我期待一个python代码来检查矩阵是否形成幻方。

eit6fx6z

eit6fx6z1#

print("WELCOME! SIR I AM MATHEMAGIX PROGRAM. \n I WILL CHECK AND TELL YOU IF YOU CAN ARRANGE THE EGGS IN THE BASKET IN THAT MANNER OR NOT.")
print("\n")
print("MAGIC SQUARE :- [A square divided into smaller squares each containing a number,\n                 such that the figures in each vertical, horizontal, and diagonal row add up to the same value.]")
print("\n")
n=int(input("ENTER THE SIZE OF SQUARE MATRIX SHAPED BASKET YOU HAVE: "))
print("\n")
print("ENTER THE NO. OF EGGS BELOW, WHICH YOU WANT IN EACH SECTION OF THE BASKET.")
print("\n")
a=[]
for i in range (n):
    b=[]
    for j in range (n):
        j=int(input("Enter The Number of EGGS = "))  
        b.append(j)
    a.append(b)
if j<=(n*n):
    print("\n")
    print("THE BASKET ARRANGEMENT is ......")
    print("\n")
    for i in range(n):
        for j in range (n):
            print(a[i][j],end=" ")
        print()
    sum1d=0
    sum2d=0
    for i in range(n):
        for j in range(n):
            if i==j:
                sum1d=sum1d+a[i][j]
            if i+j==n-1:
                sum2d=sum2d+a[i][j]
    if sum1d!=sum2d:
        z=5
    else:
        for i in range(n):
            sumr=0
            sumc=0
            for j in range(n):
                sumr=sumr+a[i][j]
                sumc=sumc+a[j][i]
            if sumr!=sum1d:
                z=1
            elif sumc!=sum1d:
                z=1
            else:
                z=0
    if z==0:
        print("\n")
        print("YES SIR! YOU CAN ARRANGE YOUR BASKET WITH THE EGGS IN THAT MANNER.\nIT WILL RESULT IN FORMING A MAGIC SQUARE.")
    else:
        print("\n")
        print("UH OH! SORRY SIR YOU CAN'T ARRANGE YOUR EGGS LIKE THAT! \nIT DOESN'T FORM A MAGIC SQUARE.")
else:
    print("\n")
    print("THE NO. OF EGGS ENTERED IN ANY SLOT HAS MORE VALUE/VALUES GREATER THAN n².(i.e.- OVER THE CAPACITY OF THAT SLOT OF THE BASKET.)\n PLEASE RE-RUN THE PROGRAM AND RE-ENTER THE VALUES OF NO. OF EGGS PROPERLY !")

相关问题