Me.Hide Open App.Path & "\out.txt" For Output As #2 Open App.Path & "\in.txt" For Input As #1
Input #1, n Call c("", n - 1, n)
Close #1 Close #2 End End Sub Sub c(a, ByVal x, ByVal ini) If ini = 0 Then Print #2, a For i = x To 1 Step -1 If ini - i >= 0 Then Call c(a & i, i, ini - i) Next End Sub --------------------- in.txt 5 --------------------- out.txt 41 32 311 221 2111 11111
Dim n As Integer Private Sub Form_Load() Me.Hide Open App.Path & "\in.txt" For Input As #1 Open App.Path & "\out.txt" For Output As #2 Input #1, n Call K(n, n, 0, "")
Close Close
End End Sub
Function K(n, ln, s, ans) If s >= n Then Print #2, ans Else
For i = 1 To n - 1 If i <= ln And (s + i) <= n Then Call K(n, i, s + i, ans & " " & i) Next
Me.Hide
回覆刪除Open App.Path & "\out.txt" For Output As #2
Open App.Path & "\in.txt" For Input As #1
Input #1, n
Call c("", n - 1, n)
Close #1
Close #2
End
End Sub
Sub c(a, ByVal x, ByVal ini)
If ini = 0 Then Print #2, a
For i = x To 1 Step -1
If ini - i >= 0 Then Call c(a & i, i, ini - i)
Next
End Sub
---------------------
in.txt
5
---------------------
out.txt
41
32
311
221
2111
11111
Dim n As Integer
回覆刪除Private Sub Form_Load()
Me.Hide
Open App.Path & "\in.txt" For Input As #1
Open App.Path & "\out.txt" For Output As #2
Input #1, n
Call K(n, n, 0, "")
Close
Close
End
End Sub
Function K(n, ln, s, ans)
If s >= n Then
Print #2, ans
Else
For i = 1 To n - 1
If i <= ln And (s + i) <= n Then Call K(n, i, s + i, ans & " " & i)
Next
End If
End Function
+++++++++
輸入: 5
輸出:
1 1 1 1 1
2 1 1 1
2 2 1
3 1 1
3 2
4 1
想了兩三天,
回覆刪除在紙上寫了無數次,
果然一次就成功^^~
Dim N As Integer
Private Sub Form_Load()
Me.Hide
Open App.Path & "\in.txt" For Input As #1
Open App.Path & "\out.txt" For Output As #2
Input #1, N
Call ABC(N - 1, N, "")
Close #2
Close #1
End
End Sub
Sub ABC(A, B, C)
If B = 0 Then
Print #2, C
Else
For i = A To 1 Step -1
If B - i >= 0 Then Call ABC(i, B - i, C & i)
Next i
End If
---------------------
in.txt
5
---------------------
out.txt
41
32
311
221
2111
11111
緣尉、arro、佑好,
回覆刪除這題果然會了遞迴後,是可以解決的,你們的程式都正確,很好。