HDU 2199 Can you solve this equation

1、二分查找,水題。

2、在0和100中二分找到0點(r-l範圍在1e-6間視爲0)

#include<cstdio>
#include<iostream>
#include<cmath>
using namespace std;

double func(double x) {		//數值 
	return 8*pow(x,4)+7*pow(x,3)+2*pow(x,2)+3*x+6;
}

void binarySearch(double n) {			//二分查找 
	double l=0,r=100;					//區間[0,100] 
	if(n<func(0) || n>func(100.0)) {
		cout<<"No solution!"<<endl;
	}else{
		while(r-l>1e-6) {
			double mid=(l+r)/2;
			if(func(mid)>n) r=mid;
			else l=mid;
		}
		printf("%.4f\n",r);
	}
}

int main()
{
	int t;
	double num;
	cin>>t;
	while(t--) {
		cin>>num;
		binarySearch(num);
	}
	return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章