转载请注明出处,谢谢http://blog.csdn.net/ACM_cxlove?viewmode=contents by---cxlove
非常裸的题。。。二分之后,求曲线交部分的面积。
我只是保存自适应Simpson模板的。。
#include <iostream> #include <cstdio> #include <cmath> using namespace std; const double eps = 1e-10; double w , h , area , a[4][10]; int k; double function (double x , double *p , double *q , double y) { double up = 0 , down = 0; for (int i = 0 ; i <= k ; i ++) { up = up * x + p[i]; down = down * x + q[i]; } return max (y , up / down); } double simpson_formula (double l , double r , double *p , double *q , double y) { return (function (l , p , q , y) + 4 * function ((l + r) / 2.0 , p , q , y) + function (r , p , q , y)) * (r - l) / 6.0; } double simpson (double l , double r , double *p , double *q , double y) { double m = (l + r) / 2.0; double ans = simpson_formula (l , r , p , q , y); if (fabs (ans - simpson_formula (l , m , p , q , y) - simpson_formula (m , r , p , q , y)) < eps) return ans; return simpson (l , m , p , q , y) + simpson (m , r , p , q , y); } double gao (double low , double high) { double mid , ans = -1; int t = 50; while (t --) { mid = (low + high) / 2.0; double now = simpson (0 , w , a[0] , a[1] , mid) - simpson (0 , w , a[2] , a[3] , mid); if (now > area) ans = mid , low = mid; else high = mid; } return ans; } int main () { #ifndef ONLINE_JUDGE freopen ("input.txt" , "r" , stdin); // freopen ("output.txt" , "w" , stdout); #endif while (scanf ("%lf %lf %lf %d" , &w , &h , &area , &k) != EOF) { for (int i = 0 ; i < 4 ; i ++) for (int j = k ; j >= 0 ; j --) { scanf ("%lf" , &a[i][j]); } printf ("%.5f\n" , -gao (-h , 0)); } return 0; }