VB 中编写函数fun(n),计算s=1+1/2!+1/3!+....+1/n!怎么写代码。求帮忙补空。

如题所述

楼主

VB

c
混了
看看这个
Function
fun(ByVal
n
As
Integer)
As
Single
Dim
i,
t
As
Integer
Dim
s
As
Single
t
=
1
For
i
=
1
To
n
t
=
t
*
i
s
=
s
+
1
/
t
Next
i
fun
=
s
End
Function
注意,调用

n不能太大了,否则
就得

dim
t
as
long
温馨提示:答案为网友推荐,仅供参考
第1个回答  2020-01-18
你提供的代码有问题,应该如此:
Function
fun(ByVal
n
As
Integer)
As
Single
Dim
i,
j,
t
As
Integer:Dim
s
As
Single
s=1
for
j=2
to
n
t=1
For
i=2
To
n
t=t*i
next
i
s=s+
1/t
Next
j
fun=
s
End
Function