ACM-二分-strange fuction

和我写的can you solve the equation基本一样,需要一些高数知识,F(x)求导,求使F'(x)等于零的x即为最小值点的横坐标,然后代入F(x)即可

下面是代码:

#include<iostream>
#include<cmath>
using namespace std;
double func1(double x)
{
 return (42*x*x*x*x*x*x + 48*x*x*x*x*x + 21*x*x + 10*x);
}
double func2(double x,double y)
{
 return (6 * x*x*x*x*x*x*x + 8 * x*x*x*x*x*x + 7 * x*x*x + 5 * x*x - x*y);
}
int main()
{
 int t;
 cin >> t;
 while (t--)
 {
  int y;
  cin >> y;
  double first, mid, tail;
  first = 0; tail = 100;
  mid = (first + tail) / 2;
   while (fabs(func1(mid) - y) > 1e-4)
   {
    if (func1(mid) > y)
    {
     tail = mid; mid = (tail + first) / 2;
    }
    else
    {
     first = mid; mid = (tail + first) / 2;
    }
   }
   printf("%.4lf\n", func2(mid,y));
 }
}

猜你喜欢

转载自blog.csdn.net/qq_40783693/article/details/80426535