' 有一个数列,其第一项为1,第二项为第一项的1/2,第三项为前两项之和的 '1/2,第四项为前三项之和的1/2,以后各项均为其前面各项之和的1/2,试求该 '数列第18项的值(保留2位小数)。 Const Line_Num = 18Private Sub Form_Load()
Dim i As Integer Dim a(1 To Line_Num) As Double Dim Total As Double a(1) = 1 Debug.Print "a(1)=:" & a(1) For i = 2 To Line_Num For j = 1 To i - 1 a(i) = a(i) + a(j) Next j a(i) = CDbl(0.5) * a(i) Debug.Print "a(" & i & "):" & a(i) Total = Total + a(i) Next i Debug.Print "Total:" & Total Total = Format(Total, "#####.00") Debug.Print "Total:" & Total End Sub
1,2 同意 zyl910(910:分儿,我来了!) Private Sub Command1_Click() Debug.Print GetItem(1, 0.5, 18) Debug.Print GetItemNo(1, 2, 3, 1000) End Sub Public Function GetItem(x1 As Double, x2 As Double, max As Long, Optional i As Long = 0) As Double 'Static i As Long i = i + 1 Dim temp As Double Dim b As Boolean b = False If i < max Then 'Debug.Print i & ": " & x1 GetItem = GetItem(x2, ((x1 + x2) / 2), max, i) Else If Not b Then GetItem = x1 b = Not b End If End If End Function Public Function GetItemNo(x1 As Double, x2 As Double, x3 As Double, max As Double, Optional i As Long = 0) As Double i = i + 1 If x1 <= max Then Debug.Print i & ": " & x1 GetItemNo x2, x3, (x1 + x2 + x3) / 2, max, i End If GetItemNo = i End Function
谢谢大家的指导,还有一个小问题,请看下面程序: Private Sub Form_Click() Dim n1, n2 As Integer n1 = 0: n2 = 0 Open "d:\ch2.dat" For Input As #1 Do While Not EOF(1) Input #1, str1$ n = Len(str1$) If n = 0 Then Exit Do If n = 4 Then n1 = n1 + 1 Loop Close #1 Print n1 Open "d:\t2.dat" For Output As #2 Print #2, n1 Close #2End Sub为什么 If n = 0 Then Exit Do 这一句不能去掉? 这句有什么作用?
'1/2,第四项为前三项之和的1/2,以后各项均为其前面各项之和的1/2,试求该
'数列第18项的值(保留2位小数)。
Const Line_Num = 18Private Sub Form_Load()
Dim i As Integer
Dim a(1 To Line_Num) As Double
Dim Total As Double
a(1) = 1
Debug.Print "a(1)=:" & a(1)
For i = 2 To Line_Num
For j = 1 To i - 1
a(i) = a(i) + a(j)
Next j
a(i) = CDbl(0.5) * a(i)
Debug.Print "a(" & i & "):" & a(i)
Total = Total + a(i)
Next i
Debug.Print "Total:" & Total
Total = Format(Total, "#####.00")
Debug.Print "Total:" & Total
End Sub
Private Sub Command1_Click()
Debug.Print GetItem(1, 0.5, 18)
Debug.Print GetItemNo(1, 2, 3, 1000)
End Sub
Public Function GetItem(x1 As Double, x2 As Double, max As Long, Optional i As Long = 0) As Double
'Static i As Long
i = i + 1
Dim temp As Double
Dim b As Boolean
b = False
If i < max Then
'Debug.Print i & ": " & x1
GetItem = GetItem(x2, ((x1 + x2) / 2), max, i)
Else
If Not b Then
GetItem = x1
b = Not b
End If
End If
End Function
Public Function GetItemNo(x1 As Double, x2 As Double, x3 As Double, max As Double, Optional i As Long = 0) As Double
i = i + 1
If x1 <= max Then
Debug.Print i & ": " & x1
GetItemNo x2, x3, (x1 + x2 + x3) / 2, max, i
End If
GetItemNo = i
End Function
Private Sub Form_Click()
Dim n1, n2 As Integer
n1 = 0: n2 = 0
Open "d:\ch2.dat" For Input As #1
Do While Not EOF(1)
Input #1, str1$
n = Len(str1$)
If n = 0 Then Exit Do
If n = 4 Then n1 = n1 + 1
Loop
Close #1
Print n1
Open "d:\t2.dat" For Output As #2
Print #2, n1
Close #2End Sub为什么 If n = 0 Then Exit Do 这一句不能去掉?
这句有什么作用?