2011年12月26日 星期一

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

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

輸入範例:197

輸出範例:是質數

9 則留言:

  1. Private Sub Form_Load()
    Me.Hide
    Dim n As Boolean
    n = True
    Open App.Path & "/in.txt" For Input As #1
    Open App.Path & "/out.txt" For Output As #2
    Input #1, x

    For i = 2 To (x - 1)
    If x Mod i = 0 Then n = False
    Next

    If n = False Then
    Print #2, "不是質數"
    Else
    Print #2, "是質數"
    End If

    Close
    Close
    End
    End Sub

    回覆刪除
  2. Private Sub Form_Load()
    Dim n As Boolean
    Me.Hide
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Input #1, x
    n = True
    For i = 2 To (x - 1)
    If x Mod i = 0 Then n = False
    Next i

    If n = False Then
    Print #2, "不是質數"
    Else
    Print #2, "是質數"
    End If

    Close #2
    Close #1
    End
    End Sub

    回覆刪除
  3. 兩個程式有錯

    如果輸入0或1呢?

    自己上網查一下吧

    回覆刪除
  4. Private Sub Form_Load()
    Dim n As Boolean
    Me.Hide
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Input #1, x
    n = False
    For i = 2 To (x - 1)
    If x Mod i = 0 Then n = True
    Next i

    If (x >= 2) And (n = False) Then
    Print #2, "是質數"
    Else
    Print #2, "不是質數"
    End If

    Close #2
    Close #1
    End
    End Sub

    回覆刪除
  5. Private Sub Form_Load()
    Me.Hide
    Dim n As Boolean
    n = True
    Open App.Path & "/in.txt" For Input As #1
    Open App.Path & "/out.txt" For Output As #2
    Input #1, x

    For i = 2 To (x - 1)
    If x Mod i = 0 Then n = False
    Next

    If x <= 1 Then n = False

    If n = False Then
    Print #2, "不是質數"
    Else
    Print #2, "是質數"
    End If

    Close
    Close
    End
    End Sub

    回覆刪除
  6. Private Sub Form_Load()
    Dim n As Boolean
    n = True
    Open App.Path & "/in.txt " For Input As 1
    Open App.Path & "/out.txt" For Output As 2
    Input #1, x
    For i = 2 To (x - 1)
    If x Mod i = 0 Then n = False
    Next
    If x = 0 Or 1 Then n = False
    If n = True Then
    Print #2, x, "是質數"
    Else
    Print #2, x, "不是質數"
    End If
    Close
    Close
    End
    End Sub

    回覆刪除
  7. 小冰、晟晟程式對了,這種小細節要多注意。

    哲哲程式有錯噢:
    要注意看輸出範例的輸出 (這個很嚴重噢
    還有你t/f顛倒了

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

    改好了:D

    回覆刪除
  9. 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 Boolean
    n = True
    Input #1, s
    For i = 2 To (s - 1)
    If s Mod i Then n = False
    Next
    If (s <= 2) And (n = False) Then
    Print #2, "不是質數"
    Else
    Print #2, "是質數"
    End If
    Close
    Close
    End
    End Sub

    回覆刪除