题目描述:
请编写函数fun,其功能时:计算并输出下列多项式的值:sn=1+1/1!+1/2!+1/3!+1/4 !+……+1/n!
详细代码:
#include<stdio.h>
float fun(int n);
int main()
{
int a;
scanf_s("%d", &a);
printf("%f", fun(a));
}
float fun(int n)
{
float sum = 1;
int cal = 1;
for (int i = 1;i <= n;i++)
{
for (int j = 1;j <= i;j++)
{
cal = cal * j;
}
sum += 1.0 / cal;
cal = 1;
}
return sum;
}
/*此段代码的调试环境为VS2019*/