|
好有意思的題目,但這問題對我最難的地方是要如何找出階乘的全部排列結果(這個是關鍵)
先秀一下我的結果,全結果如附件文檔myAns.rar
只列出最主要的程式碼,附件test.rar有全部的程式碼
- '初始化
- '
- Private Sub ini_ex()
- '階乘陣列最小的N設為1
- LowestStartNo = 1
- '階乘陣列最小的N的排列的結果為LowestFactorial
- ReDim LowestFactorial(1, 1)
- LowestFactorial(1, 1) = 1
- End Sub
复制代码- '1 2 3 ...N 取得所有的排列組合,將結果輸出至result
- 'Parameters:
- 'N - 就是上述的N
- 'Result() -將結果放到這個變數
- '這是二維陣列
- 'for example
- '1 2 3的所有排列組合是 (123) (132) (213)(231)....
- '那Result的存法就是
- '(1,1)=(123)的1
- '(1,2)=(123)的2
- '(1,3)=(123)的3
- '(2,1)=(132)的1
- '(2,2)=(132)的3
- '以此類推....
- 'HasLastFactorial - 布林值。是否有之前的排列結果。若要找N=5的所有排列組合,如果有N=4的排列組合的資料,將會運算快一些
- 'LastNo - 如果HasLastFactorial為真,有之前的排列結果,那之前排列結果的N即為LastNo,如果HasLastFactorial為假,這個值就沒有意義,可隨便亂填@@
- 'LastFactorial() - 如果HasLastFactorial為真,這個就是有之前的排列結果,如果HasLastFactorial為假,這個值就沒有意義,可隨便亂填@@
- 'Returns:
- '如果有找1~N的排列組合,則為真,否則為假
- Private Function getFactorial(N As Integer, ByRef Result() As Integer, HasLastFactorial As Boolean, LastNo As Integer, LastFactorial() As Integer) As Boolean
复制代码- '以2個1 之間有1個數,2個2 之間有2個數,2個3之間有3個數...為原則,找出由(1,1,2,2,3,3,4,4,5,5,6,6,..N,N) 2*N(N<=8) 個數組成的字符串滿足上述條件的所有組合,將結果輸出到result() as string
- 'Parameters:
- 'N - 就上述的N
- 'Factorial - 1 2 3 ...N的所有排列組合,是二維陣列(第一維是
- 'for example
- '1 2 3的所有排列組合是 (123) (132) (213)(231)....
- '那Factorial的存法就是
- '(1,1)=(123)的1
- '(1,2)=(123)的2
- '(1,3)=(123)的3
- '(2,1)=(132)的1
- '以此類推....
- 'Result() - 將答案放到這個string
- 'Returns:
- '如果有答案,則為真,否則為否
- Private Function FindAns(N As Integer, Factorial() As Integer, ByRef Result() As String) As Boolean
复制代码- '開始找答案
- Sub FunQuestiong()
- Call ini_ex
- Dim noData() As Integer
- Dim f1() As Integer
- Dim f2() As Integer
- Dim f3() As Integer
- Dim f4() As Integer
- Dim f5() As Integer
- Dim f6() As Integer
- Dim f7() As Integer
- Dim f8() As Integer
- Dim f9() As Integer
- Dim f10() As Integer
- Dim Ans1() As String
- Dim Ans2() As String
- Dim Ans3() As String
- Dim Ans4() As String
- Dim Ans5() As String
- Dim Ans6() As String
- Dim Ans7() As String
- Dim Ans8() As String
- Dim Ans9() As String
- Dim Ans10() As String
- 'N=1
- If getFactorial(1, f1(), False, 0, noData) = True Then
-
- If FindAns(1, f1, Ans1) Then
- Call ShowAns(1, Ans1)
- Else
- Call ShowNoAns(1)
- End If
- End If
- 'N=2
- If getFactorial(2, f2(), True, 1, f1) = True Then
-
- If FindAns(2, f2, Ans2) Then
- Call ShowAns(2, Ans2)
- Else
- Call ShowNoAns(2)
- End If
- End If
- 'N=3
- If getFactorial(3, f3(), True, 2, f2) = True Then
-
- If FindAns(3, f3, Ans3) Then
- Call ShowAns(3, Ans3)
- Else
- Call ShowNoAns(3)
- End If
- End If
- 'N=4
- If getFactorial(4, f4(), True, 3, f3) = True Then
-
- If FindAns(4, f4, Ans4) Then
- Call ShowAns(4, Ans4)
- Else
- Call ShowNoAns(4)
- End If
- End If
- 'N=5
- If getFactorial(5, f5(), True, 4, f4) = True Then
-
- If FindAns(5, f5, Ans5) Then
- Call ShowAns(5, Ans5)
- Else
- Call ShowNoAns(5)
- End If
- End If
- 'N=6
- If getFactorial(6, f6(), True, 5, f5) = True Then
-
- If FindAns(6, f6, Ans6) Then
- Call ShowAns(6, Ans6)
- Else
- Call ShowNoAns(6)
- End If
- End If
- 'N=7
- If getFactorial(7, f7(), True, 6, f6) = True Then
-
- If FindAns(7, f7, Ans7) Then
- Call ShowAns(7, Ans7)
- Else
- Call ShowNoAns(7)
- End If
- End If
- 'N=8
- If getFactorial(8, f8(), True, 7, f7) = True Then
-
- If FindAns(8, f8, Ans8) Then
- Call ShowAns(8, Ans8)
- Else
- Call ShowNoAns(8)
- End If
- End If
- 'N=9
- If getFactorial(9, f9(), True, 8, f8) = True Then
-
- If FindAns(9, f9, Ans9) Then
- Call ShowAns(9, Ans9)
- Else
- Call ShowNoAns(9)
- End If
- End If
- 'if u have good computer u can try run N=10
- ''N=10
- 'If getFactorial(10, f10(), True, 9, f9) = True Then
- '
- ' If FindAns(10, f10, Ans10) Then
- ' Call ShowAns(10, Ans10)
- ' Else
- ' Call ShowNoAns(10)
- ' End If
- 'End If
- Call writeTxt
- End Sub
复制代码
|
|