关于程序填空,不要改变与输入输出有关的语句?

输入一个正整数 m(0<m<10),求1! + 2! + … + m!,要求定义并调用函数fact(n)计算 n!,函数形参 n 的类型是int,函数类型是double。
输入输出示例:括号内是说明
输入
5 (m=5)
输出:
1!+2!+...+5! = 153.000000

#include <stdio.h>
double fact(int n);
int main(void)
{
int i, m;
double sum;

scanf("%d", &m);
/*---------*/
printf("1!+2!+...+%d! = %f\n", m, sum);
}

慕尼黑8549860
浏览 141回答 3
3回答

慕婉清6462132

#include&nbsp;<stdio.h>double&nbsp;fact(int&nbsp;n);int&nbsp;main(void){&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;i,&nbsp;m;&nbsp;&nbsp;&nbsp;&nbsp;double&nbsp;sum;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;scanf("%d",&nbsp;&m);&nbsp;&nbsp;&nbsp;&nbsp;sum=fact(m);&nbsp;&nbsp;&nbsp;&nbsp;printf("1!+2!+...+%d!&nbsp;=&nbsp;%f\n",&nbsp;m,&nbsp;sum);&nbsp;}&nbsp;double&nbsp;fact(int&nbsp;n){&nbsp;float&nbsp;num=0.0;&nbsp;int&nbsp;total,temp;&nbsp;total=0;&nbsp;temp=1;&nbsp;for(int&nbsp;i=1;i<=n;i++)&nbsp;{&nbsp;&nbsp;for(int&nbsp;j=1;j<=i;j++)&nbsp;&nbsp;{&nbsp;&nbsp;&nbsp;temp*=j;&nbsp;&nbsp;}&nbsp;&nbsp;total+=temp;&nbsp;&nbsp;temp=1;&nbsp;}&nbsp;num=(float)total;&nbsp;return&nbsp;num;}祝你顺利通过考试!

杨魅力

用递归写的,比较挫,但是勉强实现了。写的有点急没加注释,不好意思。但是是比较简单的,可以看懂~#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;}elseif(n==0||n==1)z=1;elsez=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或负数,无不需要向下进行return ;while(m){temp=fact(m--);sum+=temp;}printf("1!+2!+...+%d! = %f\n", m, sum);}

12345678_0001

#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;}elseif(n==0||n==1)z=1;elsez=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或负数,无不需要向下进行return ;while(m){temp=fact(m--);sum+=temp;}printf("1!+2!+...+%d! = %f\n", m, sum);}
打开App,查看更多内容
随时随地看视频慕课网APP