以下是引用lotustower在2006-7-10 1:20:55的发言:Almost-Langford numbers , written by pgc01. Option Explicit Option Base 1 Const lCol As Long = 1 ' Writes in Column A Sub AlmostLangford() Dim iDigits(), iNDigits As Integer, lRow As Long For iNDigits = 2 To 18 Step 2 ReDim iDigits(iNDigits) Call AlmostLangfordN(iDigits, lRow) Next End Sub Sub AlmostLangfordN(ByRef iDigits(), ByRef lRow As Long) Dim iPosFree As Integer, iDigitsTmp(), iDigit As Integer Dim iNDigits As Integer, i As Integer iNDigits = UBound(iDigits) Do While True 'Checks if last free position was filled If iPosFree <> 0 Then If IsEmpty(iDigits(iPosFree)) Then Exit Sub 'Determines next free position For iPosFree = iPosFree + 1 To iNDigits If IsEmpty(iDigits(iPosFree)) Then Exit For Next If iPosFree = iNDigits + 1 Then Exit Sub ' Tries all digits For iDigit = 0 To 9 If iPosFree + iDigit + 1 > iNDigits Then Exit For ' Checks if digit already used For i = 1 To iNDigits If iDigits(i) = iDigit And Not IsEmpty(iDigits(i)) Then Exit For Next ' Cannot start with zero If i > iNDigits And Not (iPosFree = 1 And iDigit = 0) Then If IsEmpty(iDigits(iPosFree + iDigit + 1)) Then ' Initialises idigitstmp ReDim iDigitsTmp(iNDigits) For i = 1 To iNDigits iDigitsTmp(i) = iDigits(i) Next iDigitsTmp(iPosFree) = iDigit iDigitsTmp(iPosFree + iDigit + 1) = iDigit ' Checks if array is full (Almost-Langford number) For i = 1 To iNDigits If IsEmpty(iDigitsTmp(i)) Then Exit For Next If i > iNDigits Then lRow = lRow + 1 Cells(lRow, lCol) = "'" & Join(iDigitsTmp, "") Else Call AlmostLangfordN(iDigitsTmp, lRow) 'iDigit + 1 End If End If End If Next iDigit Loop End Sub Congratulations!!! Very fast!!!! But 0 should be removed. |