75分错了#3 求助大佬
查看原帖
75分错了#3 求助大佬
337684
TaoYixiao楼主2023/1/3 08:52
思路是牛顿迭代,但是错了一个点
#include<bits/stdc++.h>
using namespace std;
float a,b,c,d,x1,x2,x3,p,q;
float f(float x){
	return x-(a*x*x*x+b*x*x+c*x+d)/(3*a*x*x+2*b*x+c);
} 
int main(){
	scanf("%f%f%f%f",&a,&b,&c,&d);
	p=(-b-sqrt(b*b-3*a*c))/(3*a);
	q=(-b+sqrt(b*b-3*a*c))/(3*a);
	x1=(-100+p)/2;
	x2=(p+q)/2;
	x3=(q+100)/2;
	for(;abs(f(x1)-f(f(x1)))>=0.0001;x1=f(x1)){}
	for(;abs(f(x2)-f(f(x2)))>=0.0001;x2=f(x2)){}
	for(;abs(f(x3)-f(f(x3)))>=0.0001;x3=f(x3)){}
	printf("%.2lf %.2lf %.2lf",x1,x2,x3);
	return 0;
}
2023/1/3 08:52
加载中...