练习4-7 求e的近似值 (15point(s)).c

自然常数 e 可以用级数 1+1/1!+1/2!+⋯+1/n!+⋯ 来近似计算。本题要求对给定的非负整数 n,求该级数的前 n+1 项和。

输入格式:
输入第一行中给出非负整数 n(≤1000)。

输出格式:
在一行中输出部分和的值,保留小数点后八位。

输入样例:
10

输出样例:
2.71828180

Why can’t the code in the picture pass the test??!
在这里插入图片描述
Correct code:

//   Date:2020/3/18
//   Author:xiezhg5
#include <stdio.h>
int main(void)
{
	int i,j;
	int n;
	double a,sum=1;
	scanf("%d",&n);
	for(i=1;i<=n;i++)
	{
		a=1;
		for(j=1;j<=i;j++)
		{
			a=a*j;
		}
		sum=sum+1.0/a;
	}
	printf("%.8lf\n",sum);
	return 0;
}
发布了65 篇原创文章 · 获赞 28 · 访问量 1741

猜你喜欢

转载自blog.csdn.net/qq_45645641/article/details/104946700
今日推荐