如何编这一题:计算S=1+ 1/2+1/4+1/7+1/11+1/16+1/22+1/29.,当第i项的值

来源:学生作业帮助网 编辑:六六作业网 时间:2024/12/27 00:34:30
如何编这一题:计算S=1+1/2+1/4+1/7+1/11+1/16+1/22+1/29.,当第i项的值如何编这一题:计算S=1+1/2+1/4+1/7+1/11+1/16+1/22+1/29.,当第

如何编这一题:计算S=1+ 1/2+1/4+1/7+1/11+1/16+1/22+1/29.,当第i项的值
如何编这一题:计算S=1+ 1/2+1/4+1/7+1/11+1/16+1/22+1/29.,当第i项的值

如何编这一题:计算S=1+ 1/2+1/4+1/7+1/11+1/16+1/22+1/29.,当第i项的值
第一种方式:
Private Sub Command1_Click()
Dim a As Integer
Dim i As Integer
Dim S As Double
a = 1
i = 1
S = 0
Do While (1 / a) > (1 / 10000)
a = a + (i - 1)
S = S + (1 / a)
i = i + 1
If i = 141 Then Print i; S
Loop
Print S
Print i
End Sub
第二种方式
Dim a As Integer
Dim i As Integer
Dim S As Double
a = 1
i = 1
S = 0
Do
a = a + (i - 1)
S = S + (1 / a)
i = i + 1
If i = 141 Then Print i; S
Loop While (1 / a) > (1 / 10000)
Print S
Print i
注:i为项数、S为总合.如果把while改为until则符号“>”改为“.
对了,第141项为2.35936…虽然和你说的结果有点误差,但是,这是在计算机合理的范围内,因为保存数据的需要,单精度(single)和双精度(double)在实际存储数据中都会对小数位数多的产生一定误差.