Private Sub Form_Activate() Open App.Path & "\in.txt" For Input As #1 Input #1, x ans = True For i = 2 To x ^ 0.5 If x Mod i = 0 Then ans = False Next i Close
Open App.Path & "\out.txt" For Output As #2 If ans Then Print #2, "是質數" Else Print #2, "不是質數" End If Close End Sub
Private Sub Form_Activate() Open App.Path & "\in.txt" For Input As #1 Input #1, ans Close #1 Open App.Path & "\out.txt" For Output As #1 For i = 2 To ans / 2 If ans Mod i = 0 Then Print #1, "不是質數" Exit For Else Print #1, "是質數" Exit For End If Next i Close #1 End Sub
Y揚好, If ans Mod i = 0 Then Print #1, "不是質數" Exit For Else Print #1, "是質數" Exit For End If 這個判斷中,前半是可以的,只要有一個數可以整除它,那麼就不是質數,但是,如果要說它是質數的話,還得是每一個數,都不能整除它才行。 皓好, 你的程式ok,可以參考Y揚的exit for 也可以使程式執行更快些。
Private Sub Form_Activate() ans = True Open App.Path & "\in.txt" For Input As #1 Input #1, x Prime = x For i = 2 To x - 1 If x Mod i = 0 Then ans = False Exit For End If Next i Close #1 If ans Then Open App.Path & "\out.txt" For Output As #2 Print #2, "是質數" Close #2 Else Open App.Path & "\out.txt" For Output As #2 Print #2, "不是質數" Close #2 End If End Sub
Public 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, x For i = 2 To x ^ 0.5 If x Mod i = 0 Then Print #2, " 不是質數" Exit Sub End If Next i Print #2, " 是質數" Close #2 Close #1 End End Sub
Private Sub Form_Activate()
回覆刪除Open App.Path & "\in.txt" For Input As #1
Input #1, x
ans = True
For i = 2 To x ^ 0.5
If x Mod i = 0 Then ans = False
Next i
Close
Open App.Path & "\out.txt" For Output As #2
If ans Then
Print #2, "是質數"
Else
Print #2, "不是質數"
End If
Close
End Sub
高仔,
回覆刪除close
-->
close #1
close #2
寫完整些,是好習慣。
熊掌
Private Sub Form_Activate()
回覆刪除Open App.Path & "\in.txt" For Input As #1
Input #1, ans
Close #1
Open App.Path & "\out.txt" For Output As #1
For i = 2 To ans / 2
If ans Mod i = 0 Then
Print #1, "不是質數"
Exit For
Else
Print #1, "是質數"
Exit For
End If
Next i
Close #1
End Sub
Private Sub Form_Activate()
回覆刪除Open App.Path & "\in.txt" For Input As #1
Input #1, x
isPrime = True
For i = 2 To x ^ 0.5
If x Mod i = 0 Then
isPrime = False
End If
Next i
Close #1
Open App.Path & "\out.txt" For Output As #2
If isPrime Then
Print #2, "是質數"
Else
Print #2, "不是質數"
End If
Close #2
End Sub
Y揚好,
回覆刪除If ans Mod i = 0 Then
Print #1, "不是質數"
Exit For
Else
Print #1, "是質數"
Exit For
End If
這個判斷中,前半是可以的,只要有一個數可以整除它,那麼就不是質數,但是,如果要說它是質數的話,還得是每一個數,都不能整除它才行。
皓好,
你的程式ok,可以參考Y揚的exit for 也可以使程式執行更快些。
以前做過的題目 感覺大家應該都差不多= =a
回覆刪除Private Sub Form_Activate()
ans = True
Open App.Path & "\in.txt" For Input As #1
Input #1, x
Prime = x
For i = 2 To x - 1
If x Mod i = 0 Then
ans = False
Exit For
End If
Next i
Close #1
If ans Then
Open App.Path & "\out.txt" For Output As #2
Print #2, "是質數"
Close #2
Else
Open App.Path & "\out.txt" For Output As #2
Print #2, "不是質數"
Close #2
End If
End Sub
松鼠好,
回覆刪除基本題,的確會大家做的相去不遠。你的程式是ok的。
你們幾個人的程式中,在第一個迴圈的次數上是有些差別的,
可以考慮看看。(那是數學上的差別吧,到x-1、x/2、x^0.5)
熊掌
Public 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, x
For i = 2 To x ^ 0.5
If x Mod i = 0 Then
Print #2, " 不是質數"
Exit Sub
End If
Next i
Print #2, " 是質數"
Close #2
Close #1
End
End Sub
1分34