花了好长时间,终于还是解出来了,用时0.375秒,没想到会这么快吖,哈哈!其实中间也绕了不少弯路,编程思路如下:
1,使用的是穷举法,即循环所有可能选项组合,然后对每一种组合判断是否满足所有条件。
2,循环中根据清风II兄的提示,排除了一些比较明显的错误选择。主要是事前担心循环耗时太多,想尽量减少循环次数以赢得时间,事后想起来其时也没有太大必要,因为所有组合共5^10约1000万种,在乘上每一种大约判断10次的话总循环此数也就是1亿左右,因为都是内存运算,对于现在的计算机来说还是小case了。所以就算完全穷举也不会明显影响速度。
3,有几题除了需满足本身条件外,还需满足唯一性条件,这是需要注意的。
4,昨天我一个同事居然花了一个中午的时间就给凑出答案来了(no use pc),我狂晕!不过她无法证明唯一性。
附代码和注释,给感兴趣的朋友参考:
Application.ScreenUpdating = False '关闭屏幕更新
t = Timer '记录开始时间
Dim a1%, a2%, a3%, a4%, a5%, a6%, a7%, a8%, a9%, a10%
Dim cc%, i%, t1%, t2%, tc%, t3%, t4%, t5%, t6%, t7% '定义循环变量
Dim arr(1 To 10) As Integer '定义答案数组a=1,b=2,c=3,d=4,e=5
cc = 0
For a1 = 1 To 5 '第1题,不为2
If a1 = 2 Then GoTo line1
arr(1) = a1
For a2 = 2 To 5 '第2题,不为1
arr(2) = a2
For a3 = 1 To 5 '第3题,不为2
If a3 = 2 Then GoTo line2
arr(3) = a3
For a4 = 2 To 5 '第4题,不为1
arr(4) = a4
For a5 = 1 To 5 '第5题
arr(5) = a5
For a6 = 1 To 5 '第6题
arr(6) = a6
For a7 = 1 To 4 '第7题,不为5
arr(7) = a7
For a8 = 1 To 3 '第8题,不为4,5
arr(8) = a8
For a9 = 1 To 4 '第9题
If a9 = 3 Then GoTo line3 '不为3,5
arr(9) = a9
For a10 = 1 To 5
arr(10) = a10
If arr(arr(1) + 1) <> 2 Then GoTo xxx '题1条件
If arr(arr(2) + 1) <> arr(arr(2) + 2) Then GoTo xxx '题2条件
Select Case arr(3)
Case 1
t1 = 1
Case 3
t1 = 4
Case 4
t1 = 7
Case 5
t1 = 6
End Select
If arr(3) <> arr(t1) Then GoTo xxx '题3条件1
t2 = 0
If arr(3) = arr(1) Then t2 = t2 + 1
If arr(3) = arr(4) Then t2 = t2 + 1
If arr(3) = arr(7) Then t2 = t2 + 1
If arr(3) = arr(6) Then t2 = t2 + 1
If t2 > 1 Then GoTo xxx '题3条件2,需满足唯一性
t3 = 0
For i = 1 To 10
If arr(i) = 1 Then t3 = t3 + 1
Next i
If arr(4) - 1 <> t3 Then GoTo xxx '题4条件
If arr(5) <> arr(11 - arr(5)) Then GoTo xxx '题5条件1
tc = 0
For i = 6 To 10
If arr(5) = arr(i) Then tc = tc + 1
Next i
If tc > 1 Then GoTo xxx '题5条件2,需满足唯一性
t4 = 0: t5 = 0
For i = 1 To 10
If arr(i) = 1 Then t4 = t4 + 1
If arr(i) = arr(6) + 1 Then t5 = t5 + 1
Next i
If arr(6) < 5 And t4 = t5 Then GoTo linea '题6条件
If arr(6) = 5 And t4 = 1 Then GoTo linea '题6条件,注意选5时需考虑1,2,3,4均不满足
If arr(6) = 5 And t4 > 5 Then GoTo linea '题6条件,注意选5时需考虑1,2,3,4均不满足
GoTo xxx
linea:
If Abs(arr(7) - arr(8)) <> 5 - arr(7) Then GoTo xxx '题7条件
t6 = 0
For i = 1 To 10
If arr(i) = 5 Then t6 = t6 + 1
Next i
If t4 + t6 <> arr(8) + 1 Then GoTo xxx '题8条件,简化了题目,排除不可能项
Select Case arr(9)
Case 1
t7 = 7
Case 2
t7 = 6
Case 4
t7 = 8
End Select
If 9 - arr(8) <> t7 Then GoTo xxx '题9条件
cc = cc + 1
For i = 1 To 10
Cells(i, cc) = arr(i)
Next i
xxx:
Next a10
line3:
Next a9
Next a8
Next a7
Next a6
Next a5
Next a4
line2:
Next a3
Next a2
line1:
Next a1
MsgBox "搜索完毕,共找到" & cc & "组解,用时" & Timer - t & "秒"
gHUh0EH3.rar
(10.26 KB, 下载次数: 90)
|