2010年3月23日 星期二

求係數


`(x+1)^2n之各項係數(限制條件為1 £ n £ 50n為一個正整數)。
範例:
輸入:2
輸出:1, 2, 1
輸入:3
輸出:1, 3, 3, 1
輸入:4
輸出:1, 4, 6, 4, 1
輸入:5
              輸出:1, 5, 10, 10, 5, 1

7 則留言:

  1. Private Sub Form_Load()
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Input #1, x
    For i = 0 To x
    n = x
    a1 = 1: a2 = 1
    For j = 1 To i
    a1 = a1 * j
    a2 = a2 * n
    n = n - 1
    Next j
    ans = a2 / a1
    Print #2, ans
    Next i
    Close #1
    Close #2
    End Sub

    回覆刪除
  2. 阿瑋好,
    你中間做數學的C(n取i)的地方,差點兒看不懂,不過,反正不是寫給別人看懂的,這個程式OK。
    如果要寫給別人看懂的,那個中間的地方,可以改成自訂函數,每次很單純地呼叫myC(n,i),就可以得到回傳的值,也是不錯的。

    回覆刪除
  3. Private Sub Form_Load()
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Input #1, x
    i = 1
    Print #2, 1
    Do Until i > x
    ast = 1: st = 1: q = x
    For j = 1 To i
    ast = ast * j
    st = st * q
    q = q - 1
    Next j
    Print #2, st / ast
    i = i + 1
    Loop
    Close #1
    Close #2
    End Sub

    回覆刪除
  4. ㄚ揚好,
    程式OK,試函數版吧。

    回覆刪除
  5. Private Sub Form_Load()
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Input #1, x
    For i = 0 To x
    Print #2, Cnm(x, i);
    Next i
    Close #1
    Close #2
    End Sub
    Public Function Cnm(ByVal n As Long, ByVal m As Long) As Long
    q1 = 1: q2 = 1
    For i = 1 To m
    q1 = q1 * n
    n = n - 1
    q2 = q2 * i
    Next i
    Cnm = q1 / q2
    End Function

    函數版

    回覆刪除
  6. Private Sub Form_Load()
    Open App.Path & "/in.txt" For Input As #1
    Open App.Path & "/out.txt" For Output As #2
    Input #1, N
    For i = 0 To N
    Print #2, C(N, i);
    If i <> N Then Print #2, ",";
    Next i
    Close #2
    Close #1
    End Sub
    Public Function C(N, i)
    a = 1
    b = 1
    For j = 1 To i
    a = a * (N - j + 1)
    b = b * j
    Next j
    C = a / b
    End Function

    回覆刪除
  7. Private Sub Form_Load()
    Open App.Path & "/in.txt" For Input As #1
    Open App.Path & "/out.txt" For Output As #2
    Input #1, q
    For i = 0 To q
    Print #2, C(q, i);
    Next i
    Close #2
    Close #1
    End Sub
    Public Function C(x, y)
    a = 1
    b = 1
    For j = 1 To i
    a = a * (x - y + 1)
    b = b * y
    Next j
    C = a / b
    End Function

    回覆刪除