2011年4月25日 星期一

退休的福利

老瘋是楓智高中的校長,如今要退休了,楓智高中對退休有個特殊的傳統,就是第1天可以得到一顆米,而第二天可以得到2顆米,第3天可以再拿四顆米,.....請你求第N天共得多少米?N<=1000。

輸入說明 :
有個數N。

輸出說明 :
第N天共得多少米。

範例輸入 :

1
1000
範例輸出 :
10715086071862673209484250490600018105614048117055336074437503883703510511249361224931983788156958581275946729175531468251871452856923140435984577574698574803934567774824230985421074605062371141877954182153046474983581941267398767559165543946077062914571196477686542167660429831652624386837205668069375

5 則留言:

  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
    Dim n As Integer
    Do While Not EOF(1)
    Input #1, n
    Print #2, ans(n)
    Loop

    Close #2
    Close #1
    End
    End Sub


    Function ans(ByVal a As Integer) As String
    Dim b As String
    b = 1
    For i = 1 To a
    If i > 1 Then
    b = Bmult(b, 2)
    ans = Bplus(ans, b)
    Else
    ans = b
    End If
    Next

    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

    Function Bplus(N1 As String, 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



    ------------------

    感覺是濫竽充數的一題

    回覆刪除
  2. arro好,
    這題如果是在長整數內,當然只是2的幾次方,但是,如果是1000次方的話,就是回到前面練習的大數問題了。
    原則上是沒錯,畢竟前面的大數加減乘除都練習了,但是,
    你還是會不認真去分清楚數字、字串,小地方不注意,程式一旦大了,小錯誤會超難發現的,要改。
    dim b as string
    b=1
    =>
    b="1"
    還有,1+2+4+...+2^1000=?
    2^1001-1,計算上會有不同的處理。

    回覆刪除
  3. Private Sub Form_Load()
    Me.Hide
    Dim x As Integer, y As String
    Open App.Path & "\out.txt" For Output As #2
    Open App.Path & "\in.txt" For Input As #1
    Do Until EOF(1)
    Input #1, x
    ans = 1
    y = 1
    For i = 1 To x - 1
    y = mymul(y, 2)
    ans = p(ans, y)
    Next
    Print #2, ans
    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
    --------------------------
    只是應用以前的程式竟然要寫那麼久~"~

    回覆刪除
  4. 做了老半天才知道
    規則是2^N

    我以為是
    1 2 4 16 256.....


    Private Sub Form_Load()
    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

    List1.AddItem "1"
    List1.AddItem "2"
    total = "3"

    Input #1, N

    If N = 1 Then Print #2, "1"
    If N = 2 Then Print #2, "3"

    If N > 2 Then

    T = 2

    Do
    D = M_x(List1.List(T - 1), "2")
    List1.AddItem D
    total = M_Plus(total, List1.List(T))
    T = T + 1
    Loop Until T = N

    Print #2, total

    End If

    Loop

    Close #2
    Close #1

    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))

    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

    回覆刪除
  5. 這一題和另外的那一題「兔子問題」在我看來是一樣的。
    用加法就解決了。
    用listbox,將每一個新的數字,自己加自己後,就成了新的一天的數字了。
    然後,如果是n天,就多算一天到n+1天,再將n+1天的數字減1也可以。
    (或是,算到第n天後,將第1天到第n天的數字給「大數加法」也可以)
    你們3個,靠著你們寫過大數的運算,這樣的題目都用大數運算又加又乘的,真的比賽的時候,光寫玩大數加、乘,再檢查驗証是否有小錯誤後,就要下課了吧。

    回覆刪除