编写C语言程序:s=1+x+x^2⼀2!+x^3⼀3!+...+x^n⼀n! 急啊~!求帮忙

2024-11-16 10:23:12
推荐回答(3个)
回答1:

double cal(double x,int n)
{
double s=1.0;
double t=1.0;
int i;
for (i=1;i<=n;i++)
{
t=t*x/(double)i;
s+=t;
}
return(s);
}
main()
{
double x; int n;
scanf("%Lf %d", &x,&n); // L要改成小写
printf("%Lf",cal(x,n));
}

回答2:

double cal(double x,int n)
{
double s=1.0;
double t=1.0;
int i;
for (i=1;i<=n;i++)
{
t=t*x/(double)i;
s+=t;
}
return(s);
}
main()
{
double x; int n;
scanf("%lf %d", &x,&n); printf("%lf",cal(x,n));
}

回答3:

float s=1.0,t=1,x1=1;
int i;
for(i=1;i<=n;i++)
{
t*=i;
x1*=x;

s+=x1/t;

}