2011年11月15日 星期二

摺紙

一張紙0.5cm厚

對折

1次為1cm厚
2次為2cm厚
3次為4cm厚

地球至太陽距離為1億五千萬公里
試問要折幾次才能到達。

2 則留言:

  1. Private Sub Form_Load()
    '1億五千萬公里
    Me.Hide
    mo = "150000000000"
    frs$ = "1"
    ans = 1
    Open App.Path & "\out.txt" For Output As #1
    Do
    frs = Bmult(frs, "2")
    ans = ans + 1
    Loop Until Val(frs) >= Val(mo)
    Print #1, ans
    Close
    End
    End Sub




    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

    ANS:39

    回覆刪除
  2. Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\out.txt" For Output As #2
    '1億五千萬公里 =150000000km = 150000000000cm
    ans = "1"
    total = "1"
    Do
    total = Mplus(total, total)
    ans = Mplus(ans, "1")
    Loop Until Len(total) > Len("150000000000") Or max(total, "150000000000") = total
    Print #2, ans

    Close #2
    End
    End Sub

    Function max(A, B)
    If Len(A) = Len(B) Then
    If A > B Then max = A Else max = B
    End If

    End Function



    Function Mplus(A, B)

    GO = 0

    If Len(A) > Len(B) Then
    T = A
    A = B
    B = T
    End If

    For i = 1 To Len(B) - Len(A)
    A = "0" & A
    Next i

    For i = Len(A) To 1 Step -1
    TA = Val(Mid(A, i, 1))
    TB = Val(Mid(B, i, 1))
    If TA + TB + GO >= 10 Then ans = ((TA + TB + GO) Mod 10) & ans: GO = 1 Else ans = (TA + TB + GO) & ans: GO = 0
    Next i


    If GO = 1 Then ans = "1" & ans
    Mplus = ans
    End Function


    39次

    回覆刪除