2012年7月12日 星期四

整數分割方式

對於一個正整數N而言,他的一個分割,就是N寫成若干個整數的和,但不計較書寫的順序。
請寫一個程式,輸入N 把N 所有分割列出來
輸入
7
輸出
6 1
5 2
5 1 1
4 3
4 2 1
4 1 1 1
3 3 1
3 2 2
3 2 1 1
3 1 1 1 1
2 2 2 1
2 2 1 1 1
2 1 1 1 1 1
1 1 1 1 1 1 1

3 則留言:

  1. 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
    Close
    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
    End If
    End Sub

    回覆刪除
  2. 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 s(n - 1, n, "")
    Close #2
    Close #1
    End
    End Sub
    Sub s(s1, s2, s3)
    If s2 = 0 Then
    Print #2, s3
    Else
    For i = s1 To 1 Step -1
    If s2 - i >= 0 Then Call s(i, s2 - i, s3 & i)
    Next i
    End If
    End Sub

    回覆刪除
  3. 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 x(n - 1, n, "")
    Close
    Close
    End
    End Sub
    Sub x(n1, n2, n3)
    If n2 = 0 Then
    Print #2, n3
    Else
    For i = n1 To 1 Step -1
    If n2 - i >= 0 Then Call x(i, n2 - i, n3 & i)
    Next
    End If
    End Sub

    回覆刪除