--------------------------------------- 第二題 Private Sub Form_Load() Dim a(4) As Integer For i = 1 To 4 a(i) = InputBox("X值") Next i Min = a(1) For i = 1 To 4 If a(i) < Min Then Min = a(i) Next i MsgBox Min End Sub -------------------------------------- 第五題 Private Sub Form_Load() Me.Hide Open App.Path & "\out.txt" For Output As #1 t = 0 For k = 1 To 1000 If t = f(k) Then k = k + 1 c = f(k) If k = f(c) Then Print #1, k, c: t = k Next k Close #1 End End Sub Function f(ByVal x) s = 0 For i = 2 To x - 1 If x Mod i = 0 Then s = s + i Next i f = s End Function
Private Sub Form_Load() n = InputBox("") MsgBox (f(n)) End Sub Function f(n) If n = 0 Then f = 0 If n = 1 Or 2 Then f = 1 If n > 2 Then f = f(n - 1) + f(n - 2) End If End Function
哲好, 第二題正確,但是 Min = a(1) For i = 1 To 4 你都將第1項當成最小的了,還比一次做什麼呢? 第五題,寫法錯誤。(結果有沒有正確沒試) t = 0 For k = 1 To 1000 If t = f(k) Then k = k + 1 c = f(k) If k = f(c) Then Print #1, k, c: t = k Next k 在這一段程式中,t是做什麼? 第1個 if t=f(k) 這兒又是為什麼? 然後,也沒處理重複的問題。
了解 第二題 Private Sub Form_Load() Dim a(4) As Integer For i = 1 To 4 a(i) = InputBox("X值") Next i Min = a(1) For i = 2 To 4 If a(i) < Min Then Min = a(i) Next i MsgBox Min End Sub
第五題測試值1~1000結果是 48 75 140 195 程式應該無誤,有處理重複的問題 If t = f(k) Then k = k + 1 '這段是讓他在下面If k = f(c)就先用t把k記住之後在跑下一個k時如果t = f(k)就把k+1讓他不做重複的部份 c = f(k) If k = f(c) Then Print #1, k, c: t = k
Private Sub Form_Load() n = InputBox("") MsgBox (f(n)) End Sub Function f(n) If n = 0 Then f = 0 If n = 1 Then f = 1 If n = 2 Then f = 1 If n > 2 Then f = f(n - 1) + f(n - 2) End If End Function
---------------------------------------
回覆刪除第二題
Private Sub Form_Load()
Dim a(4) As Integer
For i = 1 To 4
a(i) = InputBox("X值")
Next i
Min = a(1)
For i = 1 To 4
If a(i) < Min Then Min = a(i)
Next i
MsgBox Min
End Sub
--------------------------------------
第五題
Private Sub Form_Load()
Me.Hide
Open App.Path & "\out.txt" For Output As #1
t = 0
For k = 1 To 1000
If t = f(k) Then k = k + 1
c = f(k)
If k = f(c) Then Print #1, k, c: t = k
Next k
Close #1
End
End Sub
Function f(ByVal x)
s = 0
For i = 2 To x - 1
If x Mod i = 0 Then s = s + i
Next i
f = s
End Function
第一題
回覆刪除Private Sub Form_Load()
n = InputBox("")
MsgBox (f(n))
End Sub
Function f(n)
If n = 0 Then f = 0
If n = 1 Or 2 Then f = 1
If n > 2 Then
f = f(n - 1) + f(n - 2)
End If
End Function
哲好,
回覆刪除第二題正確,但是
Min = a(1)
For i = 1 To 4
你都將第1項當成最小的了,還比一次做什麼呢?
第五題,寫法錯誤。(結果有沒有正確沒試)
t = 0
For k = 1 To 1000
If t = f(k) Then k = k + 1
c = f(k)
If k = f(c) Then Print #1, k, c: t = k
Next k
在這一段程式中,t是做什麼?
第1個 if t=f(k) 這兒又是為什麼?
然後,也沒處理重複的問題。
晟晟好,
回覆刪除程式錯誤。
n = 1 Or 2 是什麼意思呢?
你去試試
n=1 or 2
print n
了解
回覆刪除第二題
Private Sub Form_Load()
Dim a(4) As Integer
For i = 1 To 4
a(i) = InputBox("X值")
Next i
Min = a(1)
For i = 2 To 4
If a(i) < Min Then Min = a(i)
Next i
MsgBox Min
End Sub
第五題測試值1~1000結果是
48 75
140 195
程式應該無誤,有處理重複的問題
If t = f(k) Then k = k + 1
'這段是讓他在下面If k = f(c)就先用t把k記住之後在跑下一個k時如果t = f(k)就把k+1讓他不做重複的部份
c = f(k)
If k = f(c) Then Print #1, k, c: t = k
Private Sub Form_Load()
回覆刪除n = InputBox("")
MsgBox (f(n))
End Sub
Function f(n)
If n = 0 Then f = 0
If n = 1 Then f = 1
If n = 2 Then f = 1
If n > 2 Then
f = f(n - 1) + f(n - 2)
End If
End Function
因為當 n = 1 或 n = 2 的時候他的 f 會等於 1
所以就寫在一起了
晟晟好,
回覆刪除程式正確。