时间超限怎么解决
#include <stdio.h>#include <math.h>
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;
}
double funcos( double e, double x ) {
int fact;
int flag=1,b=0;
double i,c,a,result=0;
do {
a=pow(x,b);
fact=1;
for(i=0; i<=b; i++) {
if(i==0) {
i=1.0;
}
fact*=i;
}
c=a/fact;
result+=c*flag;
flag=-flag;
b+=2;
} while(fabs(c)>=e);
return result;
}
本帖最后由 jackz007 于 2021-11-15 00:56 编辑
试试这个代码呢
#include <stdio.h>
double cosx(double x , double e)
{
long long b , i , s ;
double c , d , e , v ;
for(b = c = d = v = 1.0 , s = -1 , i = 2 ; d >= e ; i += 2 , s = - s) {
c *= x * x ;
b *= i * (i - 1) ;
e = 1.0 * c / b ;
v += s * e ;
}
return v ;
}
int main() {
double e , x ;
scanf("%lf %lf" , & e , & x) ;
printf("cos(%.2f) = %.6f\n", x, cosx(x , e)) ;
}
页:
[1]