谁能把它转换成另外一种语言?
Dim hf(55, 55) As Integer
Dim zz(2502), stopp As Boolean
Dim hfzhi As Long
Dim geshu As Long
Dim huanfangjs, hfsj As Integer '幻方阶数 幻方数据
Private Sub Command1_Click()
Timer1.Interval = 56
geshu = 0
stopp = False
Command1.Visible = False
hfsj = huanfangjs * huanfangjs
hfzhi = Int(huanfangjs * (hfsj + 1) / 2)
For temp = 1 To 2502 ' 幻方可填入的数据 (1 to 2500) 初始化
zz(temp) = True 'true为未用过
Next
For w = 1 To huanfangjs
For ww = 1 To huanfangjs
hf(w, ww) = 0
Next
Next
Call huanfang
End Sub
Private Sub Command2_Click()
Timer1.Interval = 0
stopp = True
End Sub
Private Sub Form_Load()
Picture1.AutoRedraw = True
End Sub
Private Sub Text1_Change()
huanfangjs = Val(Text1.Text)
Command1.Visible = True
End Sub
Public Sub huanfang()
Call dg(1, 1, True) 'hang
Timer1.Interval = 0
End Sub
Public Sub dg(c As Integer, d As Integer, e As Boolean) 'e: true:hang, false:lie
If e = True Then
total = 0
For w = 1 To d - 1
total = total + hf(c, w)
Next
If total + (2 * hfsj - huanfangjs + d) * (huanfangjs - d + 1) / 2 < hfzhi Then GoTo endsub
If d <> huanfangjs Then bb = hfzhi - total - (2 * hfsj - huanfangjs + d + 1) * (huanfangjs - d) / 2
Else
total = 0
For w = 1 To c - 1
total = total + hf(w, d)
Next
If total + (2 * hfsj - huanfangjs + c) * (huanfangjs - c + 1) / 2 < hfzhi Then zz(b) = True: hf(c, d) = 0: GoTo endsub
If c <> huanfangjs Then bb = hfzhi - total - (2 * hfsj - huanfangjs + c + 1) * (huanfangjs - c) / 2
End If
If bb <= 1 Then bb = 1
For b = bb To hfsj
startdg:
If b = 1 Then
teee = Int(huanfangjs / 2 + 0.5)
If c > teee And d > teee And zz(1) = True Then GoTo endsub
If d > teee And zz(1) = True Then GoTo nextb
If c > teee And zz(1) = True Then GoTo nextb
End If
If b = 2 Then
If c > d And zz(2) = True Then
GoTo nextb
End If
End If
If zz(b) = False Then GoTo nextb
hf(c, d) = b
zz(b) = False
If e = False Then GoTo ccc
total = 0
For w = 1 To d
total = total + hf(c, w)
Next
If total > hfzhi Then zz(b) = True: hf(c, d) = 0: GoTo endsub
If d = huanfangjs Then
total = 0
For w = 1 To huanfangjs
total = total + hf(c, w)
Next
If total <> hfzhi Then
zz(b) = True
GoTo nextb
Else
If c <> huanfangjs Then
Call dg((c + 1), (c), False)
zz(b) = True
Else
GoTo hf_finished
End If
End If 'd的 total = hfzhi 判断结束
Else
Call dg((c), (d + 1), (e))
zz(b) = True
End If
GoTo nextb
ccc:
total = 0
For w = 1 To c
total = total + hf(w, d)
Next
If total > hfzhi Then zz(b) = True: hf(c, d) = 0: GoTo endsub
If c = huanfangjs Then '如果幻方已经到最后一行
totall = 0
For ww = 1 To huanfangjs
totall = totall + hf(ww, d)
Next
If totall <> hfzhi Then
zz(b) = True
GoTo nextb
Else
If d <> huanfangjs Then
Call dg((d + 1), (d + 1), True)
zz(b) = True
Else
GoTo hf_finished
End If
End If
Else
Call dg((c + 1), (d), (e))
zz(b) = True
End If '结束 c=huanfangjs
GoTo nextb
hf_finished:
If c = huanfangjs And d = huanfangjs Then
totall = 0
For w = 1 To huanfangjs
totall = totall + hf(w, w)
Next
If totall <> hfzhi Then
zz(b) = True
GoTo nextb
End If
totall = 0
For w = 1 To huanfangjs
totall = totall + hf(w, huanfangjs - w + 1)
Next
If totall <> hfzhi Then
zz(b) = True
GoTo nextb
End If
geshu = geshu + 1
Picture1.Cls
Open "幻方.txt" For Append As #1
Print #1, "以下是第" + Str(geshu) + "个" + Str(huanfangjs) + "阶幻方"
Picture1.Print "以下是第" + Str(geshu) + "个" + Str(huanfangjs) + "阶幻方"
For w = 1 To huanfangjs
hfprint = " "
For ww = 1 To huanfangjs
hfprint = hfprint + Str(hf(w, ww)) + " "
Next
Print #1, hfprint
Picture1.Print
Picture1.Print hfprint
Next
Close #1
'赶快打印出来吧,叫大家看看啊!
zz(b) = True
End If
nextb:
Next
endsub:
End Sub
Private Sub hoho()
hhh = ""
For w = 1 To huanfangjs
For ww = 1 To huanfangjs
hhh = hhh + Str(hf(w, ww)) + " "
Next
hhh = hhh + " | "
Next
Label2.Caption = hhh
End Sub
Private Sub Timer1_Timer()
Dim w, ww As Integer
Dim hfprint As String
Picture2.Cls
For w = 1 To huanfangjs
hfprint = " "
For ww = 1 To huanfangjs
hfprint = hfprint + Str(hf(w, ww)) + " "
Next
Picture2.Print
Picture2.Print hfprint
Next
End Sub
这个是我2年前在我上大三的时候写的程序,现在没有时间看了。不过,我真的想看看所有的n阶幻方的样子,这个就能输出n阶的所有幻方。
具体控件大家在读程序的时候自行加进去。
谁能把这个vb程序转换成运行起来速度更快的其他语言呢?
并能不能给我找个更好的算法?我要能运行起来的源代码,不要方法(也就是说,不只要中间的核心代码,要能Ctrl + C 和 Ctrl + V 能运行起来的程序,并且要求运行起来的时间最短。
代码的思路请大家自行分析。
我的要求只有1点,运行起来最快(在windows2000或windows XP 下)
我会拿4阶幻方做测试
大家必要的时候,可以考虑并行算法(不过我个人认为可能没有这个必要吧,关键是不好控制)
改进算法是最重要的。