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 For i = 1 To N Input #1, X For j = 2 To X - 1 If YN(j) = True And YN(X - j) = True Then Print #2, j & " " & X - j: Exit For Next j
Next i Close #2 Close #1 End End Sub
Function YN(A) As Boolean '判斷是否為質數 P = True For i = 2 To A - 1 If A Mod i = 0 Then P = False Next i YN = P End Function
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, ti For i = 1 To ti Input #1, n
For j = 2 To n - 1 If CH(j) = True And CH(n - j) = True Then Print #2, j & " " & n - j: Exit For Next
Next
Close Close End End Sub
Function CH(a) As Boolean Dim aa As Boolean For i = 2 To a - 1 If a Mod i = 0 Then aa = True Next If aa = False Then CH = True
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
For i = 1 To N
Input #1, X
For j = 2 To X - 1
If YN(j) = True And YN(X - j) = True Then Print #2, j & " " & X - j: Exit For
Next j
Next i
Close #2
Close #1
End
End Sub
Function YN(A) As Boolean '判斷是否為質數
P = True
For i = 2 To A - 1
If A Mod i = 0 Then P = False
Next i
YN = P
End Function
輸入範例:
3
12
100
4
輸出範例:
5 7
3 97
2 2
佑好,
回覆刪除程式正確。
(這題是考過的題目,是嗎?)
也找找去年學長做的題目,你(們)還沒做的,拿出來做做吧。
熊掌好,
回覆刪除我們好像是考用乘的
248417 = 13 * 97 * 197
好。
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, ti
For i = 1 To ti
Input #1, n
For j = 2 To n - 1
If CH(j) = True And CH(n - j) = True Then Print #2, j & " " & n - j: Exit For
Next
Next
Close
Close
End
End Sub
Function CH(a) As Boolean
Dim aa As Boolean
For i = 2 To a - 1
If a Mod i = 0 Then aa = True
Next
If aa = False Then CH = True
End Function
「數字分解」這4個字拿來當資料夾名稱好像開VB時會存取錯誤
arro好,
回覆刪除程式正確。
改進點:
Dim aa As Boolean
For i = 2 To a - 1
If a Mod i = 0 Then aa = True
Next
If aa = False Then CH = True
這段程式檢測質數,要像你在主程式中那樣,
aa =true
for i = 2 to a\2
if a mod i = 0 then aa=false : exit for
next
ch=aa
先省掉一半的迴圈,再加上一不行就跳出,再加上後頭直接給ch當傳回值。(但是,不建議直接拿ch函數名稱當變數放在程式中,而是像你一樣先設一個暫用的變數aa)