思路:给你一个公式,求零点,从题目条件可以看出,此函数式是递减的,所以只要从两头往中间二分答案即可,注意精度问题,因为要精确到小数点后4位,<1e-6居然还WA,<1e-9才过,所以说尽量使精度高点
这里e的n次方可以用exp(n)表示,也可以用pow(M_E, n)表示
以下是math.h中定义的一些常量:
/* Definitions of useful mathematical constants
* M_E - e
* M_LOG2E - log2(e)
* M_LOG10E - log10(e)
* M_LN2 - ln⑵
* M_LN10 - ln⑽
* M_PI - pi
* M_PI_2 - pi/2
* M_PI_4 - pi/4
* M_1_PI - 1/pi
* M_2_PI - 2/pi
* M_2_SQRTPI - 2/sqrt(pi)
* M_SQRT2 - sqrt⑵
* M_SQRT1_2 - 1/sqrt⑵
AC代码:
#include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <iostream> #define LL long long using namespace std; double p, q, r, s, t, u; double fun(double x) { return p * exp(-x) + q * sin(x) + r * cos(x) + s * tan(x) + t * x * x + u; } int main() { while(scanf("%lf %lf %lf %lf %lf %lf", &p, &q, &r, &s, &t, &u) != EOF) { double first = 0, final = 1; if((fun(first) < 0 && fun(final) < 0) || (fun(first) > 0 && fun(final) > 0)) { printf("No solution\n"); continue; } while(final - first > 1e-9) { double m = first + (final - first) / 2; if(fun(m) > 0) first = m; else final = m; } printf("%.4lf\n", first); } return 0; }
原文地址:http://blog.csdn.net/u014355480/article/details/44813903