VB编程求S=1-1/2+1/3-1/4+…1/100

来源:百度知道 编辑:UC知道 时间:2024/06/01 20:46:05

Option Explicit
Private Sub Command1_Click()
Dim i As Integer, s As Single
For i = 1 To 100
s = s + (-1) ^ (i + 1) * (1 / i)
Next
Print s
End Sub
'其实只要知道-1的奇数次方为-1就可以简化很多

dim i as interger ,s
s=0
for i =1 to 100
s=s+1/i
i=-i
next

楼上的回答有点问题吧
i 是循环数 怎么能赋值为-i呢
应该这样写
dim i ,j,s as integer
s=0
j=1
for i=1 to 100
if i mod 2=0 then
s-=1/j
else
s+=1/j
endif
j+=1
next