一元三次方程你会解吗?

   日期:2020-07-15     浏览:95    评论:0    
核心提示:讲义、ppt等资料二分模拟:#include#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 *

讲义、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; 
}

牛顿迭代

卡尔丹诺公式

 
打赏
 本文转载自:网络 
所有权利归属于原作者,如文章来源标示错误或侵犯了您的权利请联系微信13520258486
更多>最近资讯中心
更多>最新资讯中心
0相关评论

推荐图文
推荐资讯中心
点击排行
最新信息
新手指南
采购商服务
供应商服务
交易安全
关注我们
手机网站:
新浪微博:
微信关注:

13520258486

周一至周五 9:00-18:00
(其他时间联系在线客服)

24小时在线客服