qq_32575047 于 2016.03.06 17:08 提问

cos(x)=x0/0!−x2/2!+x4/4!−x6/6!+⋯\cos (x) = x^0 / 0! - x^2 / 2! + x^4 / 4! - x^6 / 6! + \cdotscos(x)=x
​0
​​/0!−x
​2
​​/2!+x
​4
​​/4!−x
​6
​​/6!+⋯

double funcos( double e, double x );

#include
#include

double funcos( double e, double x );

int main()
{

double e, x;

scanf("%lf %lf", &e, &x);
printf("cos(%.2f) = %.6f\n", x, funcos(e, x));

return 0;


}

/* 你的代码将被嵌在这里 */

0.01 -3.14

cos(-3.14) = -0.999899

1个回答

caozhy      2016.03.06 19:58

 #include "stdio.h"
#include "math.h"
int fact(int x)
{
int s=1;
int i;
for(i=1;i<=x;i++)
s=s*i;
return s;
}

double funcos(double e, double x);
int _tmain(int argc, _TCHAR* argv[])
{
double e, sum, x;
scanf("%le%le", &e, &x);
sum=funcos(e,x);
printf("sum = %f\n", sum);
return 0;
}
double funcos(double e, double x)
{
double s=1;
double item;
int i=2;
int j=1;
item=(double)pow(x,i)/fact(i);
while(item>e)
{
if(j%2==1)
s=s-item;
else
s=s+item;
i=i+2;
j++;
item=(double)pow(x,i)/fact(i);
}
if(j%2==1)
s=s-item;
else
s=s+item;
return s;

}