|
楼主 |
发表于 2011-4-3 15:51
|
显示全部楼层
楼上的附件xls文件在下是用“硬”办法做出来的,期待高手帮忙用VBA简单的办法搞定。
“硬”办法是:因为电子表只有65536行,而9!=362880>65536,所以先作8!=40320(即先作不含数字9的8位数的全排列),然后根据ABC+DEF=XYZ的特点ABCDEFXYZ中A、D不可能为9,即在做好的8位数第2、3、5、6、7、8、9位分别插入数字9再组成7×40320=282240个9位数分7个sheet表装入数据,再在这7个表格中将数据每相邻3个作为一组分为三列(用mid函数),用if函数判断三列ABC+DEF=XYZ是否成立并作记号,按记号筛选合乎要求的行,将7个工作表合乎要求的数据行复制到一个新的工作表内,再用if函数判断ABC<DEF,经过筛选、升序排列最终得到结果。
如此操作费时又费力,苦不堪言!故诚请高手出手相助!!
附8位数全排列并写表的程序:
Sub PL_jiu()
For i1 = 1 To 8
PL1 = "12345678"
a1 = Mid(PL1, i1, 1)
For i2 = 1 To 7
PL2 = Mid(PL1, 1, i1 - 1) & Mid(PL1, i1 + 1, 8 - i1)
a2 = Mid(PL2, i2, 1)
For i3 = 1 To 6
PL3 = Mid(PL2, 1, i2 - 1) & Mid(PL2, i2 + 1, 7 - i2)
a3 = Mid(PL3, i3, 1)
For i4 = 1 To 5
PL4 = Mid(PL3, 1, i3 - 1) & Mid(PL3, i3 + 1, 6 - i3)
a4 = Mid(PL4, i4, 1)
For i5 = 1 To 4
PL5 = Mid(PL4, 1, i4 - 1) & Mid(PL4, i4 + 1, 5 - i4)
a5 = Mid(PL5, i5, 1)
For i6 = 1 To 3
PL6 = Mid(PL5, 1, i5 - 1) & Mid(PL5, i5 + 1, 4 - i5)
a6 = Mid(PL6, i6, 1)
For i7 = 1 To 2
PL7 = Mid(PL6, 1, i6 - 1) & Mid(PL6, i6 + 1, 3 - i6)
a7 = Mid(PL7, i7, 1)
For i8 = 1 To 1
PL8 = Mid(PL7, 1, i7 - 1) & Mid(PL7, i7 + 1, 2 - i7)
a8 = PL8
i = i + 1
code = a1 & a2 & a3 & a4 & a5 & a6 & a7 & a8
Sheet1.Cells(i, 1) = code
Next i8
Next i7
Next i6
Next i5
Next i4
Next i3
Next i2
Next i1
End Sub
[ 本帖最后由 yimin0519 于 2011-4-3 16:04 编辑 ] |
|