VB 用自定义函数(function),求1!+2!+3!+…+10!并用print显示结果?

一定要在VB里求:

用自定义函数(function),求1!+2!+3!+…+10!并用print显示结果?
Private Sub Command1_click()

End Sub
function fact(x as integer) as long

end function

Private Sub Command1_click()
dim i as integer
dim sum as long
for i=1 to 10
sum=sum+fact(i)
next
debug.print sum
End Sub

function fact(x as integer) as long
if x=1 then
fact=1
else
fact=x*fact(x-1)
end if
end function

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
虽然这样写结构清晰,但运算量大,算阶乘的时候一共要乘45次

完全可以改成
Private Sub Command1_click()
dim i as integer
dim jie as long
dim sum as long
jie=1
for i=1 to 10
jie=jie*i
sum=sum+jie
next
debug.print sum
End Sub

这样算所有阶乘一共只需要乘10次

ps: 乘法要比加法浪费多的多的CPU时间
温馨提示:内容为网友见解,仅供参考
第1个回答  2008-06-14
找一个现成的

Private Sub Command1_Click()
n = InputBox("输入自然数", "输入")
JieCheng n
If n > 1 Then
For j = n To 2 Step -1
Print j & "*";
Next j
End If
Print "1=";
Print JieCheng(n)
End Sub

Private Function JieCheng(ByVal i&) As Long
If i > 1 Then
JieCheng = i * JieCheng(i - 1)
Else
JieCheng = 1
End If
End Function

http://zhidao.baidu.com/question/55901687.html

VB 用自定义函数(function),求1!+2!+3!+…+10!并用print显示结果?
Private Sub Command1_click()dim i as integer dim sum as long for i=1 to 10 sum=sum+fact(i)next debug.print sum End Sub function fact(x as integer) as long if x=1 then fact=1 else fact=x*fact(x-1)end if end function -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-...

用VB求1!+2!+3!+……+10!的结果。
Private Sub Command1_Click()Dim i As Integer Dim sum As Long Dim n As Long n = 1 sum = 0 For i = 1 To 10 n = n * i sum = sum + n Next Label1.Caption = sum End Sub

vb编程,用于计算1!+2!+3!+...+10!
Private Sub Form_Click()n = 1 sum = 0 For i = 1 To 10 n = n * i sum = sum + n Next i Print sum End Sub

阶乘之和1+2!+3!+...+10! 用vb的function函数实现
Function JieChengSum() As Long Dim Sum As Long Dim i As Long Dim j As Long Sum = 0 j = 1 For i = 1 To 10 j = j * i Sum = Sum + j Next JieChengSum = Sum End Function 下面是函数的调用示例 Private Sub Command1_Click()MsgBox JieChengSum End Sub ...

...n! 的子过程,调用该过程,计算1!+2!+3!+……+10!之和(10分) 掌_百 ...
Private Sub Command1_Click()s = 0 For i = 1 To 10 s = s + fac(i)Next i Print "1!+2!+3!+...+10!="; s End Sub Function fac(n)f = 1 For i = 2 To n f = f * i Next i fac = f End Function

怎样用VB编写过程,计算1!+2!+3!+。。。+N!,N值由用户输入
楼上的好像错了,做了小改动!如下:Private Sub Command1_Click()Dim i As Integer Dim n As Integer Dim ans as integer Dim y As Integer=1 n = InputBox("请输入一个正整数: ")y=1 ans=0 For i = 1 To n y = y * i ans=ans+y Next i Print ans End Sub ...

VB编写程序计算1+2!+3!+4!+……+10!的程序
Function jc(n As Integer) As Long Dim i jc = 1 For i = 1 To n jc = jc * i Next End Function Private Sub Command1_Click()Dim SumAdd As Long, i For i = 1 To 10 SumAdd = SumAdd + jc(i)Next Print SumAdd End Sub ...

VB计算1+2+3+……10阶乘
As Integer) As Long Dim result As Long Dim sum As Long Dim i As Integer result = 1 sum = 0 For i = 1 To n result = result * i sum = sum + result Next i jiecheng = sum End Function 本程序已经测试成功,你只要调用就可以了,例如要计算你那道题,就调用jiecheng(10)...

vb中1!+2!+3!+……n!的代码怎么写
先输入一段代码,定义阶乘函数:Function f(n As Integer) As Long If n > 1 Then f = n * f(n - 1) Else f = 1 End IfEnd Function然后,再写一段代码,执行累加的运算,例如我要把结果在单击时输出在窗体上,且n取10时(当n取不同的值时可以改成不同的数字),可以这样写:Pr...

vb 求阶乘之和 1!+2!+3!+4!+...+20! 代码是否正确
你的程序代码有错,把n<=20改成n<=3,运行一下就知道错了,1!+2!+3!=1+2+6=9,而用你的程序代码运行的结果是15。更正的方法是:把“Item = 1”移到“Do While n <= 20 ”之后。即:Private Sub Form_Click()Dim n#,b b = 0 n = 1 Do While n <= 20 Item = 1 For i ...

相似回答
大家正在搜