给出一个N次函数,保证在范围[l,r]内存在一点x,使得[l,x]上单调增,[x,r]上单调减。试求出x的值。
看代码即可。
#include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int MAX_N = 15; const double EPS = 0.0000001; double A[MAX_N]; int N; double F(double x) { double ans = A[1]; for(int i = 2; i <= N; i++) ans = ans * x + A[i]; return ans; } int main() { double l, r; scanf("%d%lf%lf", &N, &l, &r); N++; for(int i = 1; i <= N; i++) scanf("%lf", A + i); while(r - l > EPS) { double mid1 = (r - l) / 3 + l, mid2 = (r - l) / 3 * 2 + l; if (F(mid1) > F(mid2)) r = mid2; else l = mid1; } printf("%.5f\n", l); return 0; }
原文地址:https://www.cnblogs.com/headboy2002/p/9218907.html
时间: 2024-10-11 10:43:40