一元三次方程你会解吗?

讲义、ppt等资料

二分模拟:

#include<bits/stdc++.h>
#define re register
#define f(i, a, b) for(re int i = a; i <= b; ++i)
using namespace std;

double a, b, c, d, l, r, m, x1, x2;
int cnt = 0;//特判 

inline double search(double x){
	return a * x * x * x + b * x * x + c * x + d; 
}

signed main(){
	scanf("%lf%lf%lf%lf", &a, &b, &c, &d);
	f(i, -100, 100){
		l = i;
		r = i + 1;
		x1 = search(l);
		x2 = search(r);
		if(!x1){
			printf("%.2lf ", l);
			cnt++;
		}
		if(x1 * x2 < 0){
			while(r - l >= 0.001){
				m = (l + r) / 2;
				(search(m) * search(r) <= 0) ? l = m : r = m;
			}
			printf("%.2lf ", r);
			cnt++;
		}
		if(cnt == 3) break; 
	}
	return 0;
}

盛金公式:

#include<bits/stdc++.h>
using namespace std;

double a, b, c, d, A, B, t, theta, x1, x2, x3;

signed main(){
	 cin >> a >> b >> c >> d;
	A = b * b - 3 * a * c;
	B = b * c - 9 * a * d;
	t = (2 * A * b - 3 * a * B) / (2 * sqrt(A * A * A));
	theta = acos(t);
	x1 = (-b - 2 * sqrt(A) * cos(theta / 3)) / (3 * a);
	x2 = (-b + sqrt(A) * (cos(theta / 3) + sqrt(3) * sin(theta / 3))) / (3 * a);
	x3 = (-b + sqrt(A) * (cos(theta / 3) - sqrt(3) * sin(theta / 3))) / (3 * a);
	cout << fixed << setprecision(2) << x1 << " ";
    cout << fixed << setprecision(2) << x3 << " ";
    cout << fixed << setprecision(2) << x2 << " ";
	return 0;
} 

                                                      
暴力法:

#include<bits/stdc++.h>
using namespace std;

signed main(){
	double a, b, c, d;
	int cnt = 0;
	scanf("%lf%lf%lf%lf", &a, &b, &c, &d);
	for(register double i = -100; i <= 100; i += 0.001){
		if(cnt == 3) break;
		double j = i + 0.001;
		double y1 = a * i * i * i + b * i * i + c * i + d;
		double y2 = a * j * j * j + b * j * j + c * j + d;
		if(y1 >= 0 && y2 <= 0 || y1 <= 0 && y2 >= 0){
			cnt++;
			double x = (i + j) / 2;
			printf("%.2lf ", x);
		}
	}
	return 0; 
}

牛顿迭代

卡尔丹诺公式

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值