12345678每个数字只能用一次填空……用VB解这题!
12345678每个数字只能用一次填空()+()=7()+()=9()-()=1()-()=2就是这题。我用VB写了个程序,通过穷举法穷举出1,2,3,4,5,6,7,8这8个数字的所有的组合,然后一个个来试题目中的括号,来试出所有的答案。VERSION 5.00
Begin VB.Form frmMain
AutoRedraw = -1'True
BorderStyle = 1'Fixed Single
Caption = "计算"
ClientHeight = 4200
ClientLeft = 45
ClientTop = 375
ClientWidth = 5175
LinkTopic = "Form1"
MaxButton = 0 'False
MinButton = 0 'False
ScaleHeight = 280
ScaleMode = 3'Pixel
ScaleWidth = 345
StartUpPosition = 3'窗口缺省
Begin VB.ListBox lstVals
Height = 4200
Left = 0
TabIndex = 0
Top = 0
Width = 5175
End
End
Attribute VB_Name = "frmMain"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
'这里开始,是代码的正文。
Option Explicit
Private HaveResult As Boolean
'参数:级别,排列结果,可用数字
Sub 穷举算法(ByVal Level As Long, Nums() As Long, NumAvail() As Long)
Dim I&, NewNumAvail() As Long, J&
If Level Then
For I = 0 To Level
'选出当前数字
Nums(Level) = NumAvail(I)
'重新选出下一级可用数字
ReDim NewNumAvail(Level - 1)
For J = 0 To I - 1
NewNumAvail(J) = NumAvail(J)
Next
For J = I + 1 To Level
NewNumAvail(J - 1) = NumAvail(J)
Next
'进行下一级穷举
穷举算法 Level - 1, Nums, NewNumAvail
Erase NewNumAvail
Next
Else
'最后一级数字
Nums(Level) = NumAvail(0)
'已经穷举出数字了
If Nums(0) - Nums(1) = 1 And _
Nums(2) - Nums(3) = 2 And _
Nums(4) + Nums(5) = 7 And _
Nums(6) + Nums(7) = 9 Then '如果八个数字都满足条件
'添加到列表框
Dim Strn As String
For I = 0 To UBound(Nums) - 1
Strn = Strn & Nums(I) & ","
Next
Strn = Strn & Nums(I)
lstVals.AddItem Strn
HaveResult = True
DoEvents
End If
End If
End Sub
Private Sub Form_Load()
Dim Nums(7) As Long, NumAvail(7) As Long
Show
Dim I&
For I = 0 To 7
NumAvail(I) = I + 1
Next
穷举算法 7, Nums, NumAvail
If HaveResult Then MsgBox "已经计算完成。", vbInformation Else MsgBox "已经计算完成,无解。", vbExclamation
End Sub结果是无解。
SRC:
BIN: 可以放到数学专栏去。 哈哈哈哈哈哈哈,最后我直接笑爆哈哈哈哈哈哈 一看就是无解的,()+()=2,填不了
页:
[1]