Private Sub Form_Load() Dim a(30) As Integer Open App.Path & "\in.txt" For Input As #1 Open App.Path & "\out.txt" For Output As #2 Input #1, x For j = 1 To x Input #1, num For i = 2 To num - 1 If check(i) Then If check(num - i) Then Print #2, i; " "; num - i Exit For End If End If Next i Next j Close #1 Close #2 End Sub
Public Function check(a) As Boolean check = True For i = 2 To a - 1 If a Mod i = 0 Then check = False Exit For End If Next i End Function
Private Sub Form_Load() Open App.Path & "\in.txt" For Input As #1 Open App.Path & "\out.txt" For Output As #2 Input #1, n For ii = 1 To n Input #1, x For i = 2 To x - 1 If check(i, x - i) Then Print #2, i; " "; x - i Exit For End If Next i Next ii Close #2 Close #1 End Sub Public Function check(ByVal a, ByVal b) As Boolean check = True For i = 2 To a - 1 If b Mod i = 0 Then check = False Next i For i = 2 To b - 1 If b Mod i = 0 Then check = False Next i End Function
Private Sub Form_Load() Open App.Path & "\in.txt" For Input As #1 Open App.Path & "\out.txt" For Output As #2 Input #1, aaa For i = 1 To aaa Input #1, aa For j = 2 To aa - 1 b = j a = aa - j If ans(a) = True And ans(b) = True Then Print #2, b & " " & a Exit For End If Next j Next i Close #2 Close #1 End Sub Public Function ans(x) As Boolean ans = True For i = 2 To x ^ 0.5 If x Mod i = 0 Then ans = False Exit For End If Next i End Function
Dim V As Boolean Private Sub Form_Load() 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, m For j = 2 To m \ 2 + 1 V = True Call AA(m, j) If V = True Then Print #2, j; m - j: Exit For Next j Next i Close #2 Close #1 End Sub
Public Sub AA(m, j) For i = 2 To j ^ 0.5 If j Mod i = 0 Then V = False Next i For i = 2 To (m - j) ^ 0.5 If (m - j) Mod i = 0 Then V = False Next i End Sub
Private Sub Form_Load()
回覆刪除Dim a(30) As Integer
Open App.Path & "\in.txt" For Input As #1
Open App.Path & "\out.txt" For Output As #2
Input #1, x
For j = 1 To x
Input #1, num
For i = 2 To num - 1
If check(i) Then
If check(num - i) Then
Print #2, i; " "; num - i
Exit For
End If
End If
Next i
Next j
Close #1
Close #2
End Sub
Public Function check(a) As Boolean
check = True
For i = 2 To a - 1
If a Mod i = 0 Then
check = False
Exit For
End If
Next i
End Function
試試看函數 看來是不錯用:D
回覆刪除阿揚好,
回覆刪除程式ok。
Private Sub Form_Load()
回覆刪除Open App.Path & "\in.txt" For Input As #1
Open App.Path & "\out.txt" For Output As #2
Input #1, n
For ii = 1 To n
Input #1, x
For i = 2 To x - 1
If check(i, x - i) Then
Print #2, i; " "; x - i
Exit For
End If
Next i
Next ii
Close #2
Close #1
End Sub
Public Function check(ByVal a, ByVal b) As Boolean
check = True
For i = 2 To a - 1
If b Mod i = 0 Then check = False
Next i
For i = 2 To b - 1
If b Mod i = 0 Then check = False
Next i
End Function
阿瑋好,
回覆刪除1程式中,用的變數,要嘛有意義,像是sum, avg, stu之類的,要嘛要容易區別,像x,y,z或是a,b,c。
你程式中用i, ii這樣的變數,一旦不小心打錯了,想自己找出錯誤,可以要花兩倍時間哦。
2函數在傳回值,用的是函數的名字。
所以,盡可能不要將函數的名字出現太多次,也是不容易偵錯的哦。
你可以用 ans=true
...ans=false
.....
...ans=false
最後才來一個
check = ans
3你的程式OK。(哈,唸了你一堆,程式還是對的,會想吐血嗎?)
Private Sub Form_Load()
回覆刪除Open App.Path & "\in.txt" For Input As #1
Open App.Path & "\out.txt" For Output As #2
Input #1, aaa
For i = 1 To aaa
Input #1, aa
For j = 2 To aa - 1
b = j
a = aa - j
If ans(a) = True And ans(b) = True Then
Print #2, b & " " & a
Exit For
End If
Next j
Next i
Close #2
Close #1
End Sub
Public Function ans(x) As Boolean
ans = True
For i = 2 To x ^ 0.5
If x Mod i = 0 Then
ans = False
Exit For
End If
Next i
End Function
半夜了睡不著
可能是沒有把題目做完不太甘心吧XD
(說實在...這題原本沒用Function
都弄到自己亂掉
用了Function後好做多了!!=ˇ=
話說這幾天我好像做的有點少呀
真是夠慚愧了0.0...)
小白好,
回覆刪除程式ok,
還是提醒,在函數中,不要拿函數名字在中間當變數用。
函數名字,只拿來做最後的傳回值時使用。
Dim V As Boolean
回覆刪除Private Sub Form_Load()
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, m
For j = 2 To m \ 2 + 1
V = True
Call AA(m, j)
If V = True Then Print #2, j; m - j: Exit For
Next j
Next i
Close #2
Close #1
End Sub
Public Sub AA(m, j)
For i = 2 To j ^ 0.5
If j Mod i = 0 Then V = False
Next i
For i = 2 To (m - j) ^ 0.5
If (m - j) Mod i = 0 Then V = False
Next i
End Sub