求VB程序代码 1!+2!……20!的和

求VB高手 帮小弟我解答一下这个程序怎么写,谢谢了

楼上一堆人都不知道精度是会溢出的吗?
你们自己算算20!都多少位了?10!是7位,20!不会低于18位,哪个精度也表示不了这么大数字的精确值,只能用高精度计算。

'高精度乘法
Sub Multiply(src() As Integer, multiplier As Integer)
Dim i As Integer
For i = 0 To UBound(src)
If src(i) <> 0 Then
src(i) = src(i) * multiplier
End If
Next
Carryout src
End Sub
'高精度加法
Sub Add(src() As Integer, adder() As Integer)
Dim i As Integer
For i = 0 To UBound(src)
If i <= UBound(adder) Then
src(i) = src(i) + adder(i)
End If
Next
Carryout src
End Sub
'高精度进位
Sub Carryout(src() As Integer)
Dim i As Integer
For i = 0 To UBound(src)
If src(i) > 9 Then
If i = UBound(src) Then
ReDim Preserve src(i + 1)
End If
src(i + 1) = src(i + 1) + src(i) \ 10
src(i) = src(i) Mod 10
End If
Next
End Sub

'主程序
Sub Main()
Dim i As Integer
Dim MDigit() As Integer
Dim Sum() As Integer
ReDim Sum(0), MDigit(0)
Sum(0) = 0: MDigit(0) = 1
For i = 1 To 20
Multiply MDigit, i
If UBound(MDigit) > UBound(Sum) Then
ReDim Preserve Sum(UBound(MDigit))
End If
Add Sum, MDigit
Next
Dim result As String
For i = UBound(Sum) To 0 Step -1
result = result & Sum(i)
Next
MsgBox result
End Sub

最后结果是:2561327494111820313
温馨提示:内容为网友见解,仅供参考
第1个回答  2009-05-21
Private Sub Command1_Click()
Dim s As Double, s1 As Double, i As Integer
s1 = 1
For i = 1 To 20
s1 = s1 * i
s = s + s1
Debug.Print "i="; i, s1, s
Next i
MsgBox "1!+2!……20!=" & s
End Sub
第2个回答  2009-05-21
s(1)=1
sum=1
for i=2 to 20
s(i)=s(i-1)*i
sum=sum+s(i)
next i
应该就可以了
第3个回答  2009-05-21
Dim i, k As Integer
Dim sum As Double = 0
Dim s As Double = 1
For i = 1 To 20
For k = 1 To i
s = s * k
Next
sum = sum + s
Next
MessageBox.Show(sum)
第4个回答  2009-05-21
简单之极。

Function Exclam(Num) As Single
Dim Chu As Single, Retu As Single
Retu = 1
For Chu = 1 To Num
Retu = Retu * Chu
Next
Exclam = Retu
End Function

Function 计算和() As Single
Dim Jssw As Single, Jia As Single
For Jssw = 1 to 20
Jia=Jia + Exclam(Jssw)
Next
计算和 = Jia
End Function

Private Sub Form_Click()
MsgBox 计算和
End Sub

'单击窗体即可。
第5个回答  2009-05-21
s=1
for i =1 to 20
for j= i to 1 step -1
s=s*j
next j
sum=sum+s
next i
print sum

求VB程序代码 1!+2!……20!的和
ReDim Preserve src(i + 1)End If src(i + 1) = src(i + 1) + src(i) \\ 10 src(i) = src(i) Mod 10 End If Next End Sub '主程序 Sub Main()Dim i As Integer Dim MDigit() As Integer Dim Sum() As Integer ReDim Sum(0), MDigit(0)Sum(0) = 0: MDigit(0) ...

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 ...

VB求1-20的阶乘之和
For i = 1 To 20 t = 1 For j = 1 To i t = t * j Next j sum = t + sum Next i MsgBox "1-20的阶乘之和为:" & sum, vbInformation, "确认"你要先建立一个窗体,在窗体上建个按钮,再将上述代码粘贴到按钮的过程内。

求1到20的阶乘的累加和的VB代码
private sub form_click()dim n as integer,m as long,sum as long m = 1 sum = 0 n = val(inputbox("输入一个正整数"))for i = 1 to n m = m i sum = sum + m next i print n & "的阶乘和是"& sum end sub

求1!+2!+3!+...+20!
结果:2561327494111820313。C语言位运算 有6种: &, | , ^(亦或), <<(左移), >>(右移)。注意:参与位运算的元素必须是int型或者char型,以补码形式出现。按位与& &运算常应用于:迅速清零、保留指定位、判断奇偶性。a & 1 = 1;则a为奇数 b & 1 = 0;则a为偶数 ...

vb程序设计:求1+2+3+5+8+13+……前20项和
如图所示,望采纳。。。

用VB程序设计语言描述“求10+20+30+…+300的和?”的算法。
可以使用循环来实现这个算法,具体的VB程序设计语言描述如下:Dim sum As Integer ' 定义一个变量来保存求和结果 sum = 0 ' 初始化求和结果为0 For i As Integer = 10 To 300 Step 10 ' 循环10到300,每次增加10 sum = sum + i ' 将当前循环变量的值加入求和结果中 Next MsgBox "10+20+...

VB程序代码求指教
一,解决输入并保存信息的功能:因为,要有输入信息的功能,所以要解决输入并保存信息的功能,有两种方法;1,采用VBA,在WORD或者EXCEL里。2,你自己编写文本编辑器。二,按照你的要求:1,程序要有设置时间的功能。2,每次输入信息完毕,进行文件保存。3,文件格式分为上下两大块,中间用设置的时间突出...

用VB写设计程序代码,求:s=1+(1+2)+(1+2+3)+??+(1+2+3+??+n)的值。
s = 1 i = 1 Do i = i + 1 s = s + 1 \/ i Loop Until i >= n sumD = s End Function Function sumF(n As Integer) As Single s = 0 For i = 1 To n s = s + 1 \/ i Next i sumF = s End Function Private Sub Form_Click()sum1 = sumD(100)sum2 = sumF...

vb编程计算s=1+根号2+根号3+…+根号20,在窗体上显示他们的和
Private Sub Form_Click()Dim s As Double, i As IntegerFor i = 1 To 20 s = s + Sqr(i)NextPrint sEnd Sub

相似回答
大家正在搜