自然常数 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;
}