对运行中输入的x计算级数: 1+ x – x2⼀2! + x3⼀3! - …(-1) n+1*xn⼀n!。 输出精度为10-8。

用while和for语句各编写一个程序 ,谢啦
2025-03-04 02:33:23
推荐回答(1个)
回答1:

#include
#include
#include

unsigned int jiecheng(unsigned x){
if(x==1){
return 1;
}else{
return x * jiecheng(x-1);
}
}

double fuc(double x ,unsigned int n){
unsigned int i;
float result = 1.0;
if(n==0){
return 1.0;
}
else{
for(i=1;i<=n;i++){
result += pow(-1,i+1)*pow(x,i)/jiecheng(i);
}
return result;
}
}

void main()
{
printf("%lf \n",fuc(1,0));
printf("%lf \n",fuc(1,1));
printf("%lf \n",fuc(2,10));
system("pause");
}

应该没有问题,while循环跟for循环没有区别的,自己改吧