我在制作魔方时遇到问题。魔方是一个数字的正方形,在其所有行和列上具有相同的总和。
例如: -
1 2 14
3 13 1
13 2 2
我不知道我的代码有什么问题,但它应该从magicin.txt中读取三个数字并从那里读取暴力方块并在magicout.txt中写入输出
我正在编码,因为它是在AIO练习题中。该网站是: http://orac.amt.edu.au/cgi-bin/train/problem.pl?set=aio16int&problemid=902
如果需要进一步澄清或有任何需要澄清,请发表评论。代码在这里(它有一堆无用的打印和“按Enter键退出”语句进行调试):
def Main():
file=open("magicin.txt","r")
magicin=file.read()
file.close()
num1=""
num2=""
num3=0
num4=""
num5=0
num6=0
num7=0
num8=0
num9=0
indices = [i for i, x in enumerate(magicin) if x == " "]
splitSpace2=indices[1]
splitSpace=indices[0]
for x in range(splitSpace):
num1+=magicin[x]
for x in range(splitSpace+1,splitSpace2):
num2+=magicin[x]
for x in range(splitSpace2+1,len(magicin)):
num4+=magicin[x]
num1=int(num1)
num2=int(num2)
num4=int(num4)
for x in range(20):
num3=x
sum=num1+num2+num3
num7=sum-num1
if num7<0:
print("Falied with num7="+str(num7))
else:
for y in range(20):
num5=y
for z in range(20):
num6=z
if num6+num5+num4==sum:
for a in range(20):
num8=a
if num8+num2+num5==sum:
for b in range(20):
num9=b
if num9+num8+num7==sum and num9+num6+num3==sum:
answer=[str(num1),str(num2),str(num3),str(num4),str(num5),str(num6),str(num7),str(num8),str(num9)]
file=open("magicout.txt","w")
file.write(answer[0]+" "+answer[1]+" "+answer[2]+"\n"+answer[3]+" "+answer[4]+" "+answer[5]+"\n"+answer[6]+" "+answer[7]+" "+answer[8])
file.close()
return
else:
print("failed with num9+num8+num7==sum and num9+num6+num3==sum"+" "+str(num9)+" "+str(num8)+" "+str(num7)+" "+str(sum),str(num9),str(num6),str(num3),str(sum))
else:
print("Failed with num8+num2+num5==sum",str(num8),str(num2),str(num5),str(sum))
else:
print("Failed with num6="+str(num6)+" num5="+str(num5),"num4="+str(num4),"sum="+str(sum))
if x==19:
print(num1,num2,num3,num4,num5,num6,num7,num8,num9)
wait=input("")
file=open("magicout.txt","w")
file.write("none")
file.close()
answer=Main()
谢谢
答案 0 :(得分:2)
我最近完成了这个问题,实际上看起来很容易。在普通的魔方,你不能重复任何数字。但是,AIO问题允许您根据需要重复编号。因此,出现了令人惊讶的对称性。
如果我让幻数为m
,我可以用以下形式写出我的魔方:
a b m-(a+b
c m-(b+c) b
m-(a+c) c a
因此,您可以使用简单的数学创建一个魔术方阵。您只需要测试m
,以便m-(a+c)
和m-(a+b)
中的较大者介于1和20之间。您实际需要做的就是查看是否属实,是查看是否你方块的中间数大于1.所有其他数字在任何情况下都会一直有效。从这开始,我在python中创建了以下代码:
infile = open("magicin.txt", "r")
outfile = open("magicout.txt", "w")
split = infile.readline().split()
n1 = int(split[0])
n2 = int(split[1])
n4 = int(split[2])
square = [ [n1, n2, 0], [n4, 0, 0], [0, 0, 0]]
minmagic = max(square[0][0] + square[0][1], square[0][0] + square[1][0]) + 1 #lowest possible magic number
maxmagic = max(square[0][0] + square[0][1], square[0][0] + square[1][0]) +20 #highest possible magic number
for magic in range(minmagic, maxmagic + 1):
square[2][0] = magic - (square[0][0] + square[1][0]) #fill bottom left corner
square[0][2] = magic - (square[0][0] + square[0][1]) #fill top right corner
square[2][2] = square[0][0] #fill bottom right
square[2][1] = square[1][0] #fill bottom edge
square[1][2] = square[0][1] #fill right edge
square[1][1] = magic - (square[0][1] + square[2][1]) #fill middle of square
if square[1][1] > 0: # if middle is greater than 0, you've found a square, so break the for loop.
break
outfile.write(str(square[0][0]) + " " + str(square[0][1]) + " " + str(square[0][2]) + "\n")
outfile.write(str(square[1][0]) + " " + str(square[1][1]) + " " + str(square[1][2]) + "\n")
outfile.write(str(square[2][0]) + " " + str(square[2][1]) + " " + str(square[2][2]) + "\n")
我希望这有帮助!