3 回答

TA貢獻(xiàn)1804條經(jīng)驗(yàn) 獲得超2個(gè)贊
#include <stdio.h>
double fact(int n);
int main(void)
{
int i, m;
double sum;
scanf("%d", &m);
sum=fact(m);
printf("1!+2!+...+%d! = %f\n", m, sum);
}
double fact(int n)
{
float num=0.0;
int total,temp;
total=0;
temp=1;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=i;j++)
{
temp*=j;
}
total+=temp;
temp=1;
}
num=(float)total;
return num;
}
祝你順利通過考試!

TA貢獻(xiàn)1811條經(jīng)驗(yàn) 獲得超6個(gè)贊
用遞歸寫的,比較挫,但是勉強(qiáng)實(shí)現(xiàn)了。寫的有點(diǎn)急沒加注釋,不好意思。但是是比較簡(jiǎn)單的,可以看懂~
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
double fact(int n)
{
double z=0;
if(n<0)
{
printf("\nCount Error:n=%d<0 ",n);
z=0;
}
else
if(n==0||n==1)
z=1;
else
z=fact(n-1)*n;
return(z);
}
void main()
{
int m=0;
double sum=0.0,temp=0.0;
scanf("%d", &m);
if(m<=0) //m為0或負(fù)數(shù),無不需要向下進(jìn)行
return ;
while(m)
{
temp=fact(m--);
sum+=temp;
}
printf("1!+2!+...+%d! = %f\n", m, sum);
}

TA貢獻(xiàn)1802條經(jīng)驗(yàn) 獲得超5個(gè)贊
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
double fact(int n)
{
double z=0;
if(n<0)
{
printf("\nCount Error:n=%d<0 ",n);
z=0;
}
else
if(n==0||n==1)
z=1;
else
z=fact(n-1)*n;
return(z);
}
void main()
{
int m=0;
double sum=0.0,temp=0.0;
scanf("%d", &m);
if(m<=0) //m為0或負(fù)數(shù),無不需要向下進(jìn)行
return ;
while(m)
{
temp=fact(m--);
sum+=temp;
}
printf("1!+2!+...+%d! = %f\n", m, sum);
}
- 3 回答
- 0 關(guān)注
- 175 瀏覽
添加回答
舉報(bào)