#include "stdio.h"
main()
{
float sum=0,s=1,t=1,x; /*s,t最好用浮点,要不然经常溢出,另外初值应当是1*/
int i,k=1,n;
printf("input x=");
scanf("%f",&x);
printf("\ninput n=");
scanf("%d",&n);
for(i=0;;i+=2)
{
if(i<=2*n)
{
sum+=(t/s)*k;
k*=-1;
}
else
break;
s*=(i+1)*(i+2); /*阶乘的计算要乘以两项*/
t*=x*x;
}
printf("y=%f\n",sum);
getch();
}
/*类似cos的泰勒展开式*/
谢谢采纳(*^__^*)
#include
double jc(int n)
{
double s=1;
int i;
for(i=1;i<=n;i++)
s*=i;
return s;
}
double f(double x, int n)
{
int i;
double s=0,c=1;
if(n<=0)
return 1;
for(i=1;i<=n;i++)
{
c*=(-1)*x*x/jc(2*i);
s+=c;
}
return s;
}
int main()
{
double x,y;
int n;
scanf("%lf%d",&x,&n);
printf("%lf\n",f(x,n)/(f(x+2.3,n)+f(x-3.2,n+3)));
}