输入一个数N.已知n!=n=(n-1)*(n-2)...3*2*1,编写程序计算和显示s=1!+2!+3!+...
问题描述:
输入一个数N.已知n!=n=(n-1)*(n-2)...3*2*1,编写程序计算和显示s=1!+2!+3!+...
用VB做.代码+解释思路.
答
n!=n*(n-1)*(n-2)*...*2*1 的意思就是n!等于前n个正整数相乘.不是楼主所说的 3!=3*(3-1)*(3-2)*(3-3)*3*2*1 .3!=3*2*1 (其实就是1*2*3=3!) 4!=4*3*2*1 (其实就是1*2*3*4=4!) 所以这段代码不难.Dim a As Long,n As Long,s As Long,b As Long Private Sub Command1_Click() s = 0 For b = 1 To Val(Text1.Text) a = 1 For n = 1 To b a = a * n Next n s = s + a Next b Print s End Sub 把N输入到text1里就可以了.