您的位置:首页 > 其它

Problem 1001

2016-04-07 22:24 169 查看
简单题意: 输入一个数Y,如果有一个数x(0<x<=100),使得8*x^4 + 7*x^3 + 2*x^2 + 3*x + 6 == Y,误差在1e-6以内,

解题思路:

用二分法求解,然后利用搜索求得最终解。

代码如下:

#include<iostream>

#include<stdio.h>

#include<iomanip>

#include<cmath>

using namespace std;

double fun(double x)

 {

     return 8*x*x*x*x+7*x*x*x+2*x*x+3*x+6 ;

 }

 double m(double x,double z, double y)

 {

     double mid;

     while((y-z)>1e-10)

     {

         mid=(z+y)/2;

         if(fun(mid)<x)

             z=mid+1e-10;

         else

             y=mid-1e-10;

     }

     return mid;

}

 int main()

 {

     int n;

     double a;

     cin>>n;

     while(n--)

     {

         cin>>a;

         if(a<6||a>807020306||fabs(m(a,0,100))<1e-4)

            cout<<"No solution!"<<endl;

         else

            cout<<fixed<<setprecision(4)<<m(a,0,100)<<endl;

     }

    return 0;

 }

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: