2011年5月1日 星期日

兔子問題

  




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


輸入:


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




輸入範例:
0
1




輸出範例:
2
6






參考http://zerojudge.tw/ShowProblem?problemid=d213&locale=zh_TW

11 則留言:

  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

    Do While Not EOF(1)
    Input #1, N

    Print #2, Rabbit(N)
    Loop

    Close
    Close
    End


    End Sub



    Function Rabbit(a)

    Dim cou As String, die As String
    cou = 2

    For i = 1 To a
    cou = Bmult(cou, 3)

    If i >= 10 Then
    die = 2
    For j = 1 To i - 10
    die = Bmult(die, 3)
    Next

    cou = Bless(cou, die)

    End If

    'Print cou & "-" & i
    Next


    Rabbit = cou

    End Function



    '大數加減乘法
    Function Bless(ByVal A1 As String, ByVal A2 As String)
    Dim gets As Integer, gets2 As Integer, NewA2 As String, ANS As String, NewANS As String
    '補齊
    Do Until Len(A1) = Len(A2)
    If Len(A1) > Len(A2) Then
    A2 = "0" & A2
    Else
    A1 = "0" & A1
    End If
    Loop
    ' 9'S
    For i = 1 To Len(A2)
    gets = Mid(A2, i, 1)
    NewA2 = NewA2 & (9 - gets)
    Next
    ' 10'S
    A2 = Bplus(NewA2, "1")
    '計算
    ANS = Bplus(A1, A2)

    If Len(A2) < Len(ANS) Then
    '正數
    ANS = Right(ANS, Len(ANS) - 1)
    Do While Left(ANS, 1) = 0
    ANS = Right(ANS, Len(ANS) - 1)
    Loop
    Else

    '負數
    For i = 1 To Len(ANS)
    gets2 = Mid(ANS, i, 1)
    NewANS = NewANS & (9 - gets2)
    Next

    ANS = Bplus(NewANS, "1")

    Do While Left(ANS, 1) = 0
    ANS = Right(ANS, Len(ANS) - 1)
    Loop

    ANS = "-" & ANS
    End If

    Bless = ANS
    End Function
    Function Bplus(ByVal N1 As String, ByVal N2 As String)

    Dim NS1 As Integer, NS2 As Integer, Tmp As Integer, Plus As String, FAns As String

    Do Until Len(N1) = Len(N2)
    If Len(N1) > Len(N2) Then
    N2 = "0" & N2
    Else
    N1 = "0" & N1
    End If
    Loop


    For i = Len(N1) To 0 Step -1

    If i = 0 Then
    FAns = Tmp & FAns
    Else
    NS1 = Mid(N1, i, 1)
    NS2 = Mid(N2, i, 1)
    Plus = NS1 + NS2
    FAns = ((Plus + Tmp) Mod 10) & FAns
    Tmp = (Plus + Tmp) \ 10
    End If

    Next

    If Left(FAns, 1) = "0" Then FAns = Right(FAns, Len(FAns) - 1)

    Bplus = FAns

    End Function




    Function Bmult(A1 As String, A2 As String)
    Dim NS1, NS2, L1 As Integer, L2 As Integer, Ans1 As String, Ans2 As String


    For i = Len(A2) To 1 Step -1
    NS2 = Val(Mid(A2, i, 1))
    L1 = 0: L2 = 0
    For j = Len(A1) To 1 Step -1

    NS1 = Val(Mid(A1, j, 1))

    L2 = NS1 * NS2
    Ans1 = ((L1 + L2) Mod 10) & Ans1
    L1 = (L2 + L1) \ 10

    Next
    Ans1 = L1 & Ans1
    For k = Len(A2) - i To 1 Step -1
    Ans1 = Ans1 & "0"
    Next
    If Left(Ans1, 1) = "0" Then Ans1 = Right(Ans1, Len(Ans1) - 1)
    Ans2 = Bplus(Ans1, Ans2)
    Ans1 = ""
    Next

    Bmult = Ans2

    End Function




    這個還是要用到大數
    (原來大數真好用)
    特別要注意的應該就是兔子會死掉的問題

    回覆刪除
  2. 62年時應該是
    762356181270105976724031200598 隻


    Private Sub Form_Load()
    Me.Hide


    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2

    Do While Not EOF(1)
    List1.Clear
    First = 2
    List1.AddItem First

    Input #1, Y

    For i = 1 To Y

    First = M_x(First, "3")

    If i >= 10 Then
    die = M_cut(First, List1.List((List1.ListCount) - 10))
    List1.AddItem die
    First = M_cut(First, M_cut(First, die))
    Else
    List1.AddItem First
    End If

    Next i

    Print #2, List1.List(List1.ListCount - 1)

    Loop

    Close #2
    Close #1

    End
    End Sub


    Function M_Plus(A, B)
    If Len(A) > Len(B) Then B = M_0(B, Len(A) - Len(B))
    If Len(A) < Len(B) Then A = M_0(A, Len(B) - Len(A))

    go = 0
    ans = ""

    For i = Len(A) To 1 Step -1
    C = Val(Mid(A, i, 1)) + Val(Mid(B, i, 1)) + go
    go = 0
    If C >= 10 Then go = C \ 10: C = C Mod 10
    ans = C & ans
    If go <> 0 And i = 1 Then ans = go & ans
    Next i

    M_Plus = ans

    End Function

    Function M_0(ed, T)
    For i = 1 To T
    ed = "0" & ed
    Next i
    M_0 = ed
    End Function

    Function M_x(A, B)
    If Len(A) > Len(B) Then B = M_0(B, Len(A) - Len(B))
    If Len(A) < Len(B) Then A = M_0(A, Len(B) - Len(A))

    For i = Len(A) To 1 Step -1
    For j = Len(B) To 1 Step -1

    D = Val(Mid(A, i, 1)) * Val(Mid(B, j, 1))

    If D <> 0 Then

    D = D & Zero

    ans = M_Plus(ans, D)

    End If

    Zero = Zero & "0"

    Next j
    Zero = Left(Zero, Len(A) - i + 1)
    Next i

    M_x = ans

    End Function

    Function Co9(A) '九補數 +1

    C = ""

    For i = 1 To Len(A)
    C = C & (9 - Val(Mid(A, i, 1)))
    Next i

    Co9 = M_Plus(C, "1")

    End Function

    Function M_cut(A, B) '減法
    If Len(A) > Len(B) Then B = M_0(B, Len(A) - Len(B))
    If Len(A) < Len(B) Then A = M_0(A, Len(B) - Len(A))

    X1 = Co9(B)
    X2 = M_Plus(A, X1)
    ans = ""
    If Len(X2) > Len(A) Then

    For i = 2 To (Len(A) + 1)
    ans = ans & Mid(X2, i, 1)
    Next i

    Else

    For i = 1 To Len(A)
    ans = ans & Mid(X2, i, 1)
    Next i
    ans = Co9(ans)
    ans = M_Plus(ans, "1")

    ans = "-" & ans

    End If

    If Left(ans, 1) = 0 Then ans = Replace(ans, "0", "", 1, 1)

    M_cut = ans

    End Function

    回覆刪除
  3. arro,佑好,
    你們兩個都不給你們的輸入和輸出,這樣子很累人。
    而且,我覺得你們的解題,也都怪怪的。
    我來做的話,就算是要大數問題,也全部只用一個,大數加法,就完成了。
    不用那麼複雜的。
    ***
    還有,我覺得你們用乘法,乘以3的意思是?
    寫一下,第1年到第12年的輸出吧。

    回覆刪除
  4. 我兔子那題好像有錯

    20年減掉10年前的兔子沒有減到第0年的兔子

    回覆刪除
  5. Dim an(63) As String
    Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\out.txt" For Output As #2
    Open App.Path & "\in.txt" For Input As #1

    Do Until EOF(1)
    Input #1, r
    an(0) = "2"
    If r < 10 Then
    For i = 1 To r
    an(i) = mymul(an(i - 1), "3")
    Next
    Else
    For i = 1 To 9
    an(i) = mymul(an(i - 1), "3")
    Next
    For i = 10 To r
    an(i) = cc(mymul(an(i - 1), "3"), an(i - 10))
    Next
    End If
    Print #2, an(r)
    Loop

    Close #1
    Close #2
    End
    End Sub
    Function p(m, n)
    Lenm = Len(m)
    Lenn = Len(n)
    If m = "" Or m = 0 Then m = "0"
    If n = "" Or n = 0 Then n = "0"
    Do Until i > Lenm And i > Lenn
    If Lenm > i Then x = Val(Mid(m, Lenm - i, 1)) Else x = 0
    If Lenn > i Then y = Val(Mid(n, Lenn - i, 1)) Else y = 0
    z = x + y + c
    c = z \ 10
    a = (z Mod 10) & a
    i = i + 1
    Loop
    'i 從零開始 z mod 10 多跑一次 所以去零
    If Left(a, 1) = 0 Then a = Right(a, Len(a) - 1)
    p = a
    End Function
    Function mymul(mm As String, nn As String) As String
    Dim a As String
    Dim a1(10000) As String
    For i = 1 To Len(nn)
    d = Mid(nn, Len(nn) + 1 - i, 1)
    f = 0
    For j = Len(mm) To 1 Step -1
    c = Mid(mm, j, 1)
    cd = Val(d) * Val(c) + f
    f = cd \ 10
    a1(i) = (cd Mod 10) & a1(i)
    Next j
    If f > 0 Then a1(i) = f & a1(i)
    Next i

    ze = ""
    ans = "0"
    For i = 1 To Len(nn)
    a1(i) = a1(i) & ze
    ans = p(ans, a1(i))
    ze = ze & "0"
    Next i
    If Left(ans, 1) = "0" Then ans = "0"
    mymul = ans
    End Function
    Function cc(a2, b2)
    gyn = False
    lena2 = Len(a2)
    lenb2 = Len(b2)
    If lena2 > lenb2 Then Max = a2: Min = b2
    If lena2 < lenb2 Then Max = b2: Min = a2: gyn = True
    If lena2 = lenb2 Then
    If Left(a2, 1) > Left(b2, 1) Then Max = a2: Min = b2 Else Max = b2: Min = a2: gyn = True
    End If
    If Len(Min) < Len(Max) Then Call cz(Min, Max)
    c2 = p(Max, p(bc(Min), "1"))
    c2 = Right(c2, Len(c2) - 1)
    For i = 1 To Len(c2)
    If Left(c2, 1) <> "0" Then Exit For Else c2 = Right(c2, Len(c2) - 1)
    Next i
    If gyn = True Then cc = "-" & c2 Else cc = c2
    End Function
    Sub cz(a4, b4)
    Do
    a4 = "0" & a4
    If Len(a4) = Len(b4) Then Exit Do
    Loop
    End Sub
    Function bc(a5)
    For i = 1 To Len(a5)
    b5 = b5 & (9 - Val(Mid(a5, i, 1)))
    Next i
    bc = b5
    End Function
    --------------------------------
    in.txt
    0
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    ---------------------------
    out.txt
    0
    6
    18
    54
    162
    486
    1458
    4374
    13122
    39366
    118096
    354282
    1062828

    回覆刪除
  6. Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2

    Do While Not EOF(1)
    Input #1, N
    Print #2, Rabbit(N)
    Loop

    For i = 0 To 20
    Print Rabbit(i)
    Next

    Close
    Close
    End


    End Sub



    Function Rabbit(a)
    List1.Clear
    List1.List(0) = 2

    For i = 1 To a
    List1.AddItem Bmult(List1.List(i - 1), 2)
    Next

    For i = List1.ListCount To List1.ListCount - 10 Step -1
    ANS = Bplus(ANS, List1.List(i))
    Next


    Rabbit = ANS



    End Function



    '大數加減乘法
    Function Bless(ByVal A1 As String, ByVal A2 As String)
    Dim gets As Integer, gets2 As Integer, NewA2 As String, ANS As String, NewANS As String
    '補齊
    Do Until Len(A1) = Len(A2)
    If Len(A1) > Len(A2) Then
    A2 = "0" & A2
    Else
    A1 = "0" & A1
    End If
    Loop
    ' 9'S
    For i = 1 To Len(A2)
    gets = Mid(A2, i, 1)
    NewA2 = NewA2 & (9 - gets)
    Next
    ' 10'S
    A2 = Bplus(NewA2, "1")
    '計算
    ANS = Bplus(A1, A2)

    If Len(A2) < Len(ANS) Then
    '正數
    ANS = Right(ANS, Len(ANS) - 1)
    Do While Left(ANS, 1) = 0
    ANS = Right(ANS, Len(ANS) - 1)
    Loop
    Else

    '負數
    For i = 1 To Len(ANS)
    gets2 = Mid(ANS, i, 1)
    NewANS = NewANS & (9 - gets2)
    Next

    ANS = Bplus(NewANS, "1")

    Do While Left(ANS, 1) = 0
    ANS = Right(ANS, Len(ANS) - 1)
    Loop

    ANS = "-" & ANS
    End If

    Bless = ANS
    End Function
    Function Bplus(ByVal N1 As String, ByVal N2 As String)

    Dim NS1 As Integer, NS2 As Integer, Tmp As Integer, Plus As String, FAns As String

    Do Until Len(N1) = Len(N2)
    If Len(N1) > Len(N2) Then
    N2 = "0" & N2
    Else
    N1 = "0" & N1
    End If
    Loop


    For i = Len(N1) To 0 Step -1

    If i = 0 Then
    FAns = Tmp & FAns
    Else
    NS1 = Mid(N1, i, 1)
    NS2 = Mid(N2, i, 1)
    Plus = NS1 + NS2
    FAns = ((Plus + Tmp) Mod 10) & FAns
    Tmp = (Plus + Tmp) \ 10
    End If

    Next

    If Left(FAns, 1) = "0" Then FAns = Right(FAns, Len(FAns) - 1)

    Bplus = FAns

    End Function




    Function Bmult(A1 As String, A2 As String)
    Dim NS1, NS2, L1 As Integer, L2 As Integer, Ans1 As String, Ans2 As String


    For i = Len(A2) To 1 Step -1
    NS2 = Val(Mid(A2, i, 1))
    L1 = 0: L2 = 0
    For j = Len(A1) To 1 Step -1

    NS1 = Val(Mid(A1, j, 1))

    L2 = NS1 * NS2
    Ans1 = ((L1 + L2) Mod 10) & Ans1
    L1 = (L2 + L1) \ 10

    Next
    Ans1 = L1 & Ans1
    For k = Len(A2) - i To 1 Step -1
    Ans1 = Ans1 & "0"
    Next
    If Left(Ans1, 1) = "0" Then Ans1 = Right(Ans1, Len(Ans1) - 1)
    Ans2 = Bplus(Ans1, Ans2)
    Ans1 = ""
    Next

    Bmult = Ans2

    End Function










    輸入:
    0
    1
    2
    10
    輸出:

    2
    6
    14
    4092

    回覆刪除
  7. Dim an(63) As String
    Dim bir As String
    Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\out.txt" For Output As #2
    Open App.Path & "\in.txt" For Input As #1

    Do Until EOF(1)
    Input #1, r
    an(0) = "2"
    bir = "2"
    If r < 10 Then
    For i = 1 To r
    bir = mymul(bir, "2")
    an(i) = p(bir, an(i - 1))
    Next
    Else
    For i = 1 To 9
    bir = mymul(bir, "2")
    an(i) = p(bir, an(i - 1))
    Next
    For i = 10 To r
    bir = mymul(bir, "2")
    an(i) = cc(p(bir, an(i - 1)), an(i - 10))
    Next
    End If
    Print #2, an(r)
    Loop

    Close #1
    Close #2
    End
    End Sub
    Function p(m, n)
    Lenm = Len(m)
    Lenn = Len(n)
    If m = "" Or m = 0 Then m = "0"
    If n = "" Or n = 0 Then n = "0"
    Do Until i > Lenm And i > Lenn
    If Lenm > i Then x = Val(Mid(m, Lenm - i, 1)) Else x = 0
    If Lenn > i Then y = Val(Mid(n, Lenn - i, 1)) Else y = 0
    z = x + y + c
    c = z \ 10
    a = (z Mod 10) & a
    i = i + 1
    Loop
    'i 從零開始 z mod 10 多跑一次 所以去零
    If Left(a, 1) = 0 Then a = Right(a, Len(a) - 1)
    p = a
    End Function
    Function mymul(mm As String, nn As String) As String
    Dim a As String
    Dim a1(10000) As String
    For i = 1 To Len(nn)
    d = Mid(nn, Len(nn) + 1 - i, 1)
    f = 0
    For j = Len(mm) To 1 Step -1
    c = Mid(mm, j, 1)
    cd = Val(d) * Val(c) + f
    f = cd \ 10
    a1(i) = (cd Mod 10) & a1(i)
    Next j
    If f > 0 Then a1(i) = f & a1(i)
    Next i

    ze = ""
    ans = "0"
    For i = 1 To Len(nn)
    a1(i) = a1(i) & ze
    ans = p(ans, a1(i))
    ze = ze & "0"
    Next i
    If Left(ans, 1) = "0" Then ans = "0"
    mymul = ans
    End Function
    Function cc(a2, b2)
    gyn = False
    lena2 = Len(a2)
    lenb2 = Len(b2)
    If lena2 > lenb2 Then Max = a2: Min = b2
    If lena2 < lenb2 Then Max = b2: Min = a2: gyn = True
    If lena2 = lenb2 Then
    If Left(a2, 1) > Left(b2, 1) Then Max = a2: Min = b2 Else Max = b2: Min = a2: gyn = True
    End If
    If Len(Min) < Len(Max) Then Call cz(Min, Max)
    c2 = p(Max, p(bc(Min), "1"))
    c2 = Right(c2, Len(c2) - 1)
    For i = 1 To Len(c2)
    If Left(c2, 1) <> "0" Then Exit For Else c2 = Right(c2, Len(c2) - 1)
    Next i
    If gyn = True Then cc = "-" & c2 Else cc = c2
    End Function
    Sub cz(a4, b4)
    Do
    a4 = "0" & a4
    If Len(a4) = Len(b4) Then Exit Do
    Loop
    End Sub
    Function bc(a5)
    For i = 1 To Len(a5)
    b5 = b5 & (9 - Val(Mid(a5, i, 1)))
    Next i
    bc = b5
    End Function
    -----------------------------
    in.txt
    0
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    -----------------------------
    out.txt
    2
    6
    14
    30
    62
    126
    254
    510
    1022
    2046
    4092
    8182
    16360
    32714

    回覆刪除
  8. arro好,
    你的程式應該正確的。
    而緣尉,你的程式的輸出看來,應該是錯的吧。
    4092的下一年,應該是8184,想一想。
    先用筆算一算。

    回覆刪除
  9. Dim an(63)
    Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\out.txt" For Output As #2
    Open App.Path & "\in.txt" For Input As #1

    Do Until EOF(1)
    Input #1, r
    an(0) = "2"
    bir = 2
    If r < 10 Then
    For i = 1 To r
    bir = bir * 2
    an(i) = p(Str(bir), an(i - 1))
    Next
    Else
    For i = 1 To 9
    bir = bir * 2
    an(i) = p(Str(bir), an(i - 1))
    Next
    For i = 10 To r
    an(i) = p(an(i - 1), an(i - 1))
    Next
    End If
    Print #2, an(r)
    Loop

    Close #1
    Close #2
    End
    End Sub
    Function p(m, n)
    Lenm = Len(m)
    Lenn = Len(n)
    If m = "" Or m = 0 Then m = "0"
    If n = "" Or n = 0 Then n = "0"
    Do Until i > Lenm And i > Lenn
    If Lenm > i Then x = Val(Mid(m, Lenm - i, 1)) Else x = 0
    If Lenn > i Then y = Val(Mid(n, Lenn - i, 1)) Else y = 0
    z = x + y + c
    c = z \ 10
    a = (z Mod 10) & a
    i = i + 1
    Loop
    'i 從零開始 z mod 10 多跑一次 所以去零
    Do
    If Left(a, 1) = 0 Then a = Right(a, Len(a) - 1) Else Exit Do
    Loop
    p = a
    End Function
    -----------------+
    in.txt
    0
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    ------------------
    out.txt

    2
    6
    14
    30
    62
    126
    254
    510
    1022
    2046
    4092
    8184
    16368
    32736

    回覆刪除
  10. Private Sub Form_Load()
    Me.Hide


    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2

    Do While Not EOF(1)
    List1.Clear
    List2.Clear
    First = 2
    total = 2
    List1.AddItem First
    List2.AddItem First

    Input #1, Y

    For i = 1 To Y

    List1.AddItem (M_Plus(List1.List(i - 1), List1.List(i - 1)))
    total = M_Plus(List1.List(i), total)
    If List1.ListCount > 10 Then total = M_cut(total, List1.List(i - 10))
    List2.AddItem total

    Next i

    Print #2, List2.List(List2.ListCount - 1)

    Loop

    Close #2
    Close #1

    End
    End Sub


    Function M_Plus(A, B)
    If Len(A) > Len(B) Then B = M_0(B, Len(A) - Len(B))
    If Len(A) < Len(B) Then A = M_0(A, Len(B) - Len(A))

    go = 0
    ans = ""

    For i = Len(A) To 1 Step -1
    C = Val(Mid(A, i, 1)) + Val(Mid(B, i, 1)) + go
    go = 0
    If C >= 10 Then go = C \ 10: C = C Mod 10
    ans = C & ans
    If go <> 0 And i = 1 Then ans = go & ans
    Next i

    M_Plus = ans

    End Function

    Function M_0(ed, T)
    For i = 1 To T
    ed = "0" & ed
    Next i
    M_0 = ed
    End Function

    '
    Function Co9(A) '九補數 +1

    C = ""

    For i = 1 To Len(A)
    C = C & (9 - Val(Mid(A, i, 1)))
    Next i

    Co9 = M_Plus(C, "1")

    End Function

    Function M_cut(A, B) '減法
    If Len(A) > Len(B) Then B = M_0(B, Len(A) - Len(B))
    If Len(A) < Len(B) Then A = M_0(A, Len(B) - Len(A))

    X1 = Co9(B)
    X2 = M_Plus(A, X1)
    ans = ""
    If Len(X2) > Len(A) Then

    For i = 2 To (Len(A) + 1)
    ans = ans & Mid(X2, i, 1)
    Next i

    Else

    For i = 1 To Len(A)
    ans = ans & Mid(X2, i, 1)
    Next i
    ans = Co9(ans)
    ans = M_Plus(ans, "1")

    ans = "-" & ans

    End If

    If Left(ans, 1) = 0 Then ans = Replace(ans, "0", "", 1, 1)

    M_cut = ans

    End Function


    in.txt
    0
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    ------------------
    out.txt

    2
    6
    14
    30
    62
    126
    254
    510
    1022
    2046
    4092
    8184
    16368
    32736

    回覆刪除
  11. 緣尉、佑好,
    由於你們也將該有的輸入輸出給貼出來,可以理解你們的程式都正確。
    但是,這題你們都用了之前的大數解法,真正在寫程式時,會很辛苦的。
    還有,這題看來看去,怎麼看都只是題意要理解的問題吧。
    ***
    這題還有一個好玩的,因為你們都用大數解法,這一題的解題,程式貼的超長的,還蠻嚇人的,也算是進展嗎? 哈。

    回覆刪除