2011年4月11日 星期一

第4題.質因數分解

從輸入檔in4.txt中,讀入1正的長整數,請將此數分解成質因數的乘積,輸出到out4.txt

輸入範例: in4.txt
248417
輸出範例: out4.txt
248417 = 13 * 97 * 197

11 則留言:

  1. Private Sub Form_Load()
    Me.Hide
    Dim N As Long, ch As Boolean, Ans As String, chh

    Open App.Path & "\in4.txt" For Input As #1
    Open App.Path & "\out4.txt" For Output As #2

    Input #1, N

    For i = 2 To N

    If N Mod i = 0 Then

    ch = True

    For j = 2 To (i / 2)
    If i Mod j = 0 Then ch = False
    Next

    If ch = True Then
    chh = chh * i
    If chh < N Then Ans = Ans & " * " & i
    If chh = N Then Ans = Ans & " * " & i: Exit For
    If chh > N Then Ans = "": Exit For
    End If
    End If
    Next

    Ans = N & " = " & Right(Ans, Len(Ans) - 3)
    Print #2, Ans

    Close #2
    Close #1

    End
    End Sub

    回覆刪除
  2. Arro好
    你的程式錯誤囉
    輸入24時,輸出卻不等於2*2*3

    回覆刪除
  3. Dim m(100000)
    Private Sub Form_Load()
    Open App.Path & "\out4.txt" For Output As #2
    Open App.Path & "\in4.txt" For Input As #1

    Input #1, n
    o = n
    If o = 1 Then
    ans = n & " = " & 1
    Else
    If o Mod 2 = 0 Then
    m(c) = 2
    c = c + 1
    End If
    For i = 2 To o - 1
    If n Mod i = 0 Then
    For j = i - 1 To 2 Step -1
    If i Mod j = 0 Then a = 0: Exit For
    a = i
    Next j
    m(c) = a
    c = c + 1
    End If
    Next i
    For i = 0 To c
    If m(i) <> 0 Then
    If o Mod m(i) = 0 Then
    o = o / m(i)
    ans = ans & m(i) & " * "
    i = -1
    End If
    End If
    Next i
    ans = n & " = " & Left(ans, Len(ans) - 3)
    End If
    Print ans
    Close #1
    Close #2
    End Sub

    回覆刪除
  4. 緣尉

    你忘了 ME.HIDE 跟 END 了
    還有輸出到 OUT4.TXT

    回覆刪除
  5. '24=2*2*2*3

    x = 24
    y=x
    do
    for i = 2 to x
    if x mod i = 0 then
    list1.additem i
    x = x \ i
    exit for
    end if
    next i
    loop until x = 1

    回覆刪除
  6. 更正


    Private Sub Form_Load()
    Me.Hide
    Dim N As Long, ch As Boolean, Ans As String, chh

    Open App.Path & "\in4.txt" For Input As #1
    Open App.Path & "\out4.txt" For Output As #2

    Input #1, N
    X = N

    Do
    For i = 2 To N
    If N Mod i = 0 Then
    ch = True
    For j = 2 To (i / 2)
    If i Mod j = 0 Then ch = False
    Next
    If ch = True Then N = N \ i: Ans = Ans & " * " & i: Exit For
    End If
    Next
    Loop Until N = 1

    Ans = X & " = " & Right(Ans, Len(Ans) - 3)
    Print #2, Ans

    Close #2
    Close #1

    End
    End Sub

    回覆刪除
  7. Private Sub Form_Load()
    Dim N As Long
    Dim ans As String
    Me.Hide
    Open App.Path & "\in4.txt" For Input As #1
    Open App.Path & "\out4.txt" For Output As #2

    Input #1, N
    ans = N & " = "

    Do
    For i = 2 To N
    If N Mod i = 0 Then ans = ans & i & "*": N = N / i
    Next i
    Loop Until N = 1




    Print #2, Left(ans, Len(ans) - 1)

    Close #2
    Close #1

    End
    End Sub

    回覆刪除
  8. Private Sub Form_Load()
    Dim N As Long
    Dim ans As String
    Me.Hide
    Open App.Path & "\in4.txt" For Input As #1
    Open App.Path & "\out4.txt" For Output As #2

    Input #1, N
    ans = N & " = "

    Do
    For i = 2 To N
    If N Mod i = 0 Then ans = ans & i & "*": N = N / i:Exit for
    Next i
    Loop Until N = 1




    Print #2, Left(ans, Len(ans) - 1)

    Close #2
    Close #1

    End
    End Sub

    回覆刪除
  9. 佑好,
    兩個程式應該都會得到結果,只是順序不同,是嗎?
    但是,兩個程式的for迴圈中,都去更改了終點的那個變數,很容易搞混掉。
    可以試試用兩個變數,但是,似乎也沒有比較簡單就是了。

    回覆刪除
  10. 熊掌好,

    了解
    一開始先把
    N2=N

    再用N2去更改是嗎?

    回覆刪除
  11. 佑好,
    你說的可以,但是也是要實作才清楚,反正只是不想在迴圈的中間,又去更動迴圈的起點、終點或step值而已。
    Do
    n2=n
    For i = 2 To n2
    If N Mod i = 0 Then ans = ans & i & "*": N = N / i:Exit for
    Next i
    Loop Until N = 1

    雖然沒有什麼實質的幫助,但是在計概的考題中,常常看到無意義的在迴圈的裡面去更動這幾個條件的值,看了實在是有些不舒服而已 。

    回覆刪除