Problem 1001

来源:互联网 发布:微视频神器软件 编辑:程序博客网 时间:2024/05/21 22:21

简单题意: 输入一个数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;
 }



0 0
原创粉丝点击