2012年9月8日 星期六

兔子問題

若一隻兔子的壽命是10年。如果開始有一對剛生的兔子,以後每年出生兔子數是前一年出生兔子數的兩倍,求第n年兔子的總數。


輸入:


 每行一個n(0<=n<63)




輸入範例:
0
1




輸出範例:
2
6

1 則留言:

  1. Dim a(63)
    Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    List1.AddItem 2
    List1.AddItem 6
    a(0) = 2
    a(1) = 6
    For i = 2 To 63
    g = ""
    If Len(a(i - 1)) <> Len(a(i - 2)) Then
    Do
    If Len(a(i - 1)) > Len(a(i - 2)) Then a(i - 2) = "0" & a(i - 2)
    If Len(a(i - 1)) < Len(a(i - 2)) Then a(i - 1) = "0" & a(i - 1)
    Loop Until Len(a(i - 1)) = Len(a(i - 2))
    End If
    f = 0
    b = ""
    For j = Len(a(i - 1)) To 1 Step -1
    c = Val(Mid(a(i - 1), j, 1))
    d = Val(Mid(a(i - 2), j, 1))
    e = c - d - f
    f = 0
    If e < 0 And j <> 1 Then
    e = e + 10
    f = f + 1
    b = e & b
    Else
    b = e & b
    End If
    Next
    b = Val(b)

    For j = Len(b) To 1 Step -1
    c = Val(Mid(b, j, 1))
    d = Val(Mid(b, j, 1))
    e = c + d + f
    f = 0
    If e >= 10 And j <> 1 Then
    f = e \ 10
    e = e Mod 10
    g = e & g
    Else
    g = e & g
    End If
    Next
    If Len(g) <> Len(a(i - 1)) Then
    Do
    If Len(g) < Len(a(i - 1)) Then g = "0" & g
    If Len(g) > Len(a(i - 1)) Then a(i - 1) = "0" & a(i - 1)
    Loop Until Len(g) = Len(a(i - 1))
    End If
    f = 0

    For k = Len(g) To 1 Step -1
    c = Val(Mid(g, k, 1))
    d = Val(Mid(a(i - 1), k, 1))
    e = c + d + f
    f = 0
    If e >= 10 And k <> 1 Then
    f = e \ 10
    e = e Mod 10
    a(i) = e & a(i)
    Else
    a(i) = e & a(i)
    End If
    Next
    If i Mod 10 = 0 Then
    If Len(a(i)) <> Len(a(i - 10)) Then
    Do
    If Len(a(i)) > Len(a(i - 10)) Then a(i - 10) = "0" & a(i - 10)
    If Len(a(i)) < Len(a(i - 10)) Then a(i) = "0" & a(i)
    Loop Until Len(a(i - 10)) = Len(a(i))
    End If

    f = 0
    b = ""
    For j = Len(a(i)) To 1 Step -1
    c = Val(Mid(a(i), j, 1))
    d = Val(Mid(a(i - 10), j, 1))
    e = c - d - f
    f = 0
    If e < 0 And j <> 1 Then
    e = e + 10
    f = f + 1
    b = e & b
    Else
    b = e & b
    End If
    Next
    a(i) = b
    End If
    List1.AddItem a(i)
    Next
    Do While Not EOF(1)
    Input #1, x
    Print #2, List1.List(x)
    Loop
    Close
    Close
    End
    End Sub
    把大數加法、減法又再重新練習了一次。
    輸入:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12

    輸出:
    2
    6
    14
    30
    62
    126
    254
    510
    1022
    2046
    4092
    8184
    16368

    回覆刪除