讲义、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;
}
牛顿迭代
卡尔丹诺公式