您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

求1!+2!+3!+......+20!

2018/11/14 14:58:10发布108次查看
你是要用什么语言写呀
用vb帮你写吧 这个简单
dim a as integer
dim b as integer
for a = 1 to 20
b=b+a
next a
print 1+2+3…+20= & b
vb 程序
static sum as double
dim total as double
sum = 1
total = 0
for i = 1 to 20
for j = 1 to i
sum = sum * j '这里是求单个数的阶乘的
next j
total = total + sum '这里是求1!+2!+3!+......+20!的总合的
sum = 1
next i
print total
代码优化了一下,能精确的加到22!。
#include
using namespace std;
main()
{
unsigned int c[3]={1,0,0},m=2,t[3]={1,0,0},temp;
for(;m {
t[0]*=m;
t[1]*=m;
t[2]*=m;
t[1]+=(t[0]/10000000);
t[0]%=10000000;
t[2]+=(t[1]/10000000);
t[1]%=10000000;
c[0]+=t[0];
if(c[0]>10000000)
{
++c[1];
c[0]%=10000000;
}
c[1]+=t[1];
if(c[1]>10000000)
{
++c[2];
c[1]%=10000000;
}
c[2]+=t[2];
}
cout return 0;
}
调用一个递归函数 fac(n) 和累加函数sum(n)即可
起代码如下:
void sum(int n) /* 计算1的阶乘至10的阶乘的累加之和 */
{
int j;
long add; /*长整型add 存放结果 */
add=0;
for(j=1;j {
add=add+fac(j);
}
printf(\add=%ld,add); /* 输出结果 */
}
long fac(int n) /* 递归函数 */
{
if(n reture(1); /* 1的阶乘等于1 */
else
return(n*fac(n-1)) /* 求阶乘 ,采用递归方式 */
}
main()
{
sun(20);
}
c语言
mian()
{int i,j,s;
j=1;
s=0;
for(i=1;i {j=j*i;
s=s+j;
}
printf(1!+2!+……+20!= %d,s);
}
vb程序
在form1的click事件中写如下代码:
j=1
s=0
for i=1 to 20
j=j*i
s=s+j
next i
print 1!+2!+……20!=;s
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product