2012年11月20日 星期二

5/21程式小考_1st


 輸入一數n,請輸出n數之後的下一個質數。
輸入範例:6 
               45
輸出範例:7
        47

5 則留言:

  1. Dim num, mycount As Integer
    Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Do
    Input #1, num
    Do
    num = num + 1
    For i = 1 To num
    If num Mod i = 0 Then mycount = mycount + 1
    If mycount > 2 Then mycount = 0: Exit For
    Next
    If mycount = 2 Then Print #2, num: Exit Do
    Loop
    Loop Until EOF(1)
    Close #2
    Close #1
    End
    End Sub

    回覆刪除
  2. 程式錯誤
    in:
    13
    13
    6
    5
    6
    12

    out:
    17
    17
    11
    7
    11
    17

    true out:
    17
    17
    7
    7
    7
    13

    回覆刪除
  3. Dim num, mycount As Integer
    Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Do
    Input #1, num
    Do
    num = num + 1
    For i = 1 To num
    If num Mod i = 0 Then mycount = mycount + 1
    If mycount > 2 Then mycount = 0: Exit For
    Next
    If mycount = 2 Then Print #2, num: mycount = 0: Exit Do
    Loop
    Loop Until EOF(1)
    Close #2
    Close #1
    End
    End Sub

    回覆刪除
  4. Dim s As Boolean
    Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Do Until EOF(1)
    Input #1, x
    Do
    x = x + 1
    s = False
    For i = 2 To x \ 2
    If x Mod i = 0 Then s = True: Exit For
    Next
    Loop Until s = False
    Print #2, x
    Loop
    Close
    Close
    End
    End Sub

    回覆刪除
  5. Dim y As Boolean
    Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Do
    Input #1, n
    Do
    n = n + 1
    For i = 2 To n - 1
    If n Mod i = 0 Then y = False: Exit For Else y = True
    Next
    If y = True Then Exit Do
    Loop
    Print #2, n
    Loop Until EOF(1)
    Close #2
    Close #1
    End
    End Sub

    回覆刪除