我10楼的代码还能优化,把简单的判断置前,复杂的判断置后,速度还能提高:
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条件
If arr(5) <> arr(11 - arr(5)) Then GoTo xxx '题5条件1
If Abs(arr(7) - arr(8)) <> 5 - arr(7) Then GoTo xxx '题7条件
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
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条件
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,需满足唯一性
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,需满足唯一性
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:
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 & "秒"
运算时间0.33秒,呵呵!是不是有点吹毛求疵?不过严格的要求嘛应该是酱紫滴。 [em05]
[此贴子已经被作者于2005-8-29 8:43:16编辑过] |