2011年1月10日 星期一

2010第一次儲備選手選拔試卷第二題

判斷質數:從指定目錄"in.txt"讀取一數字,判斷是否為質數輸出至指定目錄"out.txt"。
("是質數","不是質數")

輸入範例:197

輸出範例:是質數

8 則留言:

  1. Private Sub Form_Load()
    Me.Hide

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

    Input #1, x

    If x = 2 Then

    a = 2
    Else
    For i = 2 To x - 1
    If x Mod i = 0 Then

    a = 1
    Exit For

    Else

    a = 2

    End If
    Next i
    End If

    If a = 1 Then Print #2, "不是質數"
    If a = 2 Then Print #2, "是質數"

    Close #1
    Close #2
    End
    End Sub

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

    For i = 2 To que - 1
    If que Mod i = 0 Then ans = False
    Next i

    If ans <> False Then Print #2, "是質數" Else Print #2, "不是質數"

    Close #2
    Close #1
    End
    End Sub

    回覆刪除
  3. Private Sub Form_Load()

    Me.Hide
    Open App.Path & "\in.txt" For Input As #1
    Input #1, a
    Close #1

    checks = 0
    For i = 2 To a / 2
    If a Mod i = 0 Then checks = 1: Exit For
    Next

    Open App.Path & "\out.txt" For Output As #2
    If checks = 0 Then Print #2, "是質數" Else Print #2, "不是質數"

    Close #2
    End
    End Sub

    回覆刪除
  4. 緣尉、hummer,arro好,
    三個程式都正確。但是arro最好jimmer次之。
    熊掌ipad

    回覆刪除
  5. Dim A As Integer
    Dim P As Boolean

    Private Sub Form_Load()

    Open App.Path & "\in.txt" For Input As #1
    Input #1, A
    Close #1

    For i = 2 To (A - 1)
    If A Mod i = 0 Then P = True: Exit For
    Next i

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

    If P = True Then
    Print #2, "不是質數"
    Else
    Print #2, "是質數"
    End If

    Close #2

    End
    End Sub

    回覆刪除
  6. 佑好,
    程式正確,變數p的初值,設一下比較好。
    熊掌

    回覆刪除
  7. 老師好,
    因為學到boolean 預設為false
    下次會注意,謝謝

    回覆刪除
  8. Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Input #1, n
    ch = 0
    For i = 2 To n - 1
    If n Mod i = 0 Then ch = 1
    Next
    If ch = 0 Then Print #2, "是質數" Else Print #2, "不是質數"
    Close
    Close
    End
    End Sub


    1:33

    回覆刪除