二分_B

来源:互联网 发布:php开发实战教程 编辑:程序博客网 时间:2024/05/16 15:55

Description

现在,给出等式8* X^4+ 7* X^3+ 2* X^2+ 3 * X +6= Y,请找出他在0和100之间的解(包含0和100)。
现在,请你试试运气。。。。
 

Input

输入的第一行包含一个整数T(1 <= T <=100),表示测试用例的数目。接下来T个数字,每一行都有一个实数Y(abs(Y)<=10^10);

Output

对于每个测试用例,如果有解,你应该输出一个实数(精确到小数点后4位,四舍五入),如果在0到100之间无解,就输出“No solution!”。

Sample Input

2

100

-4 

Sample Output

1.6152

No solution!


这是一个单调函数,在0~100利用二分找出,注意精度,一般1e-7即可


#include<iostream>#include"math.h"#include<cstdio>using namespace std;#define EPS 1e-5double f(double x){  return 8*x*x*x*x+7*x*x*x+2*x*x+3*x+6;}double binary(double y){  double low=0.0,height=100.0;  double mid=(low+height)/2.0;  while(fabs(y-f(mid))>EPS){    mid=(low+height)/2.0;    if(f(mid)<y)  low=mid;    else   height=mid;  }  return mid;}int main(){  int T;  double a;  cin>>T;  while(T--){    cin>>a;    if(f(0)>a||f(100)<a) {cout<<"No solution!"<<endl;continue;}    printf("%0.4lf\n",binary(a));  }  return 0;}





0 0
原创粉丝点击