Can you solve this equation?

作者: qwq 分类: ACM 发布时间: 2017-07-12 19:14

 

Now,given the equation 8*x^4 + 7*x^3 + 2*x^2 + 3*x + 6 == Y,can you find its solution between 0 and 100;
Now please try your lucky.

InputThe first line of the input contains an integer T(1<=T<=100) which means the number of test cases. Then T lines follow, each line has a real number Y (fabs(Y) <= 1e10);OutputFor each test case, you should just output one real number(accurate up to 4 decimal places),which is the solution of the equation,or “No solution!”,if there is no solution for the equation between 0 and 100.Sample Input

2
100
-4

Sample Output

1.6152
No solution!

这个题比较简单,就是使用最普通的二分法来解决,但是解决的时候要注意精度问题。

#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
#define eps 1e-8

int findx(double x,double y)
{
	if(y-(8.0*pow(x,4)+7.0*pow(x,3)+2.0*pow(x,2)+3.0*x+6)<0)
	   return 1;
	else
	   return 0;
}

int main(){
	double y;
	int a;
	cin>>a;
	while(a--){
		cin>>y;
		if((807020306)<y||y<6){
			cout<<"No solution!"<<endl;
			
		}else{
			//8*x^4+7*x^3+2*x^2+3*x+6==Y
			double l=0.0;
			double r=100.0;
			double mid=50.0;
			while(l+eps<r){
				if(findx(mid,y)){
					r=mid;
					mid=(l+r)/2;
				}else{
					l=mid;
					mid=(l+r)/2;
				}
				
			} 
			
			printf("%.4lf\n",l);
		}
	}
	

	return 0;
}

 

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注