ACM-二分-Can you solve this equation?


题意,思路很清楚,附上代码:

#include<iostream>
#include<cmath>
using namespace std;
double func(double x)
{
 return (8 * x*x*x*x + 7 * x*x*x + 2 * x*x + 3 * x + 6);
}
int main()
{
 int t;
 cin >> t;
 while (t--)
 {
  int y;
  cin >> y;
  double first, mid, tail;
  first = 0; tail = 100;
  if (func(first) > y||func(tail)<y)
  {
   cout << "No solution!" << endl; continue;
  }
  else
  {
   mid = (first + tail) / 2;
   while (fabs(func(mid) - y) > 1e-4)
   {
    if (func(mid) > y)
    {
     tail = mid; mid = (tail + first) / 2;
    }
    else
    {
     first = mid; mid = (tail + first) / 2;
    }
   }
   printf("%.4lf\n", mid);
  }
 }
 system("pause");
}


猜你喜欢

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