公告版位
星落的瞬間!放棄的後悔是永遠!


題目:鍵盤輸入一個自然數K(K>1),若存在自然數M和N(M>N),使得K^M 和 K^N 均大於或等於1000,且它們的末三尾數相等,則M和N是
一對”K尾相等數"。請邊一程序,輸出M+N值最小的K尾相等數。
首先以25來看
25 *25 = 625
625 * 25 = 15625 總共是三次方  由這邊看 可以知道M = 3
15625 * 25 = 390625 這邊的話可以知道 n = 4
m + n = 7
測試1:輸入 25 輸出7
測試2:輸入125 輸出 6
測試3:輸入1000 輸出3
測試4:輸入1234 輸出56
測試5:輸入111111 輸出52
測試6:輸入1000003 輸出102
測試7:輸入123454321 輸出27

附了原始檔,還沒編譯喔!
ICON是剪動漫CG的,
我剪了一大堆用XD。
經白箱測試,沒有發現問題。
原始碼:

Dim take(1000) As String

Dim nx() As String, mx(1000) As Long, l(1000) As Long, ans(1000) As Long

Private Sub Command1_Click()

    g = Val(T1)

    If g < 2 Or Fix(g) <> g Then

        MsgBox("輸入錯誤", 15, "")

        Exit Sub

    End If

    Dim K As String, Nm As String, Nn As String, m As Integer, n As Integer, Mix As Integer, flag As Boolean

    K = T1

    n = 1

    m = 1

    Nn = K

    Nm = K

    Mix = 0

    Do Until Val(Nn) >= 1000

        Nn = BigMul(Nn, K)

        n = n + 1

    Loop

    take(0) = Nn

    flag = False

    Do Until flag = True

        Mix = Mix + 1

        take(Mix) = BigMul(Nn, K)

        Nn = take(Mix)

        For i = 0 To Mix - 1

            If Cmp(take(i), take(Mix)) = True Then

                flag = True

                Exit For

            End If

        Next

    Loop

    Label2 = Str(i + Mix + 2 * n)

 

End Sub

 

Public Function Cmp(ByVal a, ByVal b) As Boolean

    If Right(a, 3) = Right(b, 3) Then

        Cmp = True

    Else

        Cmp = False

    End If

End Function

 

Public Function BigMul(ByRef a, ByVal b) As String

    x = ""

    For i = 1 To 1000

        ans(i) = 0

    Next

    Do While Left(a, 1) = 0

        a = Right(a, Len(a) - 1)

    Loop

    Do While Left(b, 1) = 0

        b = Right(b, Len(b) - 1)

    Loop

    If Len(a) < Len(b) Then

        t = a : a = b : b = t

    End If

    lea = Len(a)

    leb = Len(b)

    For i = 1 To lea

        mx(i) = Val(Right(a, 1))

        a = Left(a, Len(a) - 1)

    Next

    For i = 1 To leb

        l(i) = Right(b, 1)

        b = Left(b, Len(b) - 1)

    Next

    For i = 1 To leb

        For j = 1 To lea

            ans(j + i - 1) = ans(j + i - 1) + l(i) * mx(j)

        Next

    Next

    For i = 1 To lea + leb

        Do While ans(i) > 9

            ans(i + 1) = ans(i + 1) + 1

            ans(i) = ans(i) - 10

        Loop

    Next

    For i = 1 To lea + leb

        x = Str(ans(i)) + x

    Next

    x = Replace(x, " ", "")

    Do While Left(x, 1) = 0

        x = Right(x, Len(x) - 1)

    Loop

    BigMul = x

End Function

讓地獄深紅的天亮 發表在 痞客邦 PIXNET 留言(1) 人氣()


留言列表 (1)

發表留言
  • kya9988
  • K尾數相等?

    我終於看懂它的意思了,但是它是做什麼用的,在生活上還是數學or VB.時用的?我以前數學讀的超爛,希望大大點解QQ..
  • 說真的沒什麼用,就跟acm神手只能比賽沒人要用一樣。

    讓地獄深紅的天亮 於 2008/11/18 21:43 回覆