疯狂的罗尔定理超时求调 orz
查看原帖
疯狂的罗尔定理超时求调 orz
1025061
gzr_100楼主2024/11/20 21:31
//先找到二阶导函数的零点C2
//再找到一阶导函数的两个零点C11,C12
//再找到原函数的三个零点x1,x2,x3
//那么需要先写一个找零点的函数
#include<iostream>
#include<iomanip>
#include<cmath>
using namespace std;

int a,b,c,d;

double func(double x,int i)
{
	if(i==0) return a*x*x*x+b*x*x+c*x+d;
	else if(i==1) return 3*a*x*x+2*b*x+c;
	else return 6*a*x+2*b;
}

//bool is_find(double x,int i)
//{
//	double pre = floor( x*100 ) / 100;
//	double post = ceil( x*100 ) / 100;
//	double pre_func = func(pre, i);
//	double post_func = func(post, i);
//	double x_func = func(x, i);
//	pre_func = pre_func>0? pre_func:-pre_func;
//	post_func = post_func>0? post_func:-post_func;
//	x_func = x_func>0? x_func:-x_func;
//	return x_func<pre_func&&x_func<post_func;
//}

double find_zero(double l, double r, int i)
{
	double  idx;
	while(l<=r)
	{
		double mid = (l+r) / 2;
		int l_sign = func(l, i)>0? 1:-1;
		int r_sign = func(r, i)>0? 1:-1;
		int mid_sign = func(mid, i)>0? 1:-1;
		
//		if(is_find(mid, i))
//		{
//			idx = mid;
//			break;
//		}

		if((r-l)<0.01)
		{
			idx = mid;
			break;
		}

		if(l_sign*mid_sign < 0) r=mid;
		else if(r_sign*mid_sign < 0) l=mid;
		
	}
	return idx;
}
int main()
{
	cin>>a>>b>>c>>d;
	double C2 = find_zero(-100, 100, 2);
	double C11 = find_zero(-100, C2, 1), C12 = find_zero(C2, 100, 1);
	double x1 = find_zero(-100, C11, 0);
	double x2 = find_zero(C11, C12, 0);
	double x3 = find_zero(C12, 100, 0);
	cout<<fixed<<setprecision(2)<<x1<<" "<<x2<<" "<<x3<<endl;
	return 0;
}

其中,判定条件round(r*100)/100 == round(l*100)/100(r-l)<0.001都试过,超时。

求路过的大神帮忙看看。

2024/11/20 21:31
加载中...