题解 P1024 【一元三次方程求解】
officeyutong
2017-10-17 13:23:29
#蒟蒻的题解
直接使用盛金公式求出方程的三个实根然后输出即可
关于盛金公式是什么 可以看这里https://baike.baidu.com/item/%E7%9B%9B%E9%87%91%E5%85%AC%E5%BC%8F
```cpp
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <vector>
#include <string.h>
#include <set>
#include <cmath>
#include <iomanip>
using namespace std;
using int_t = long long int;
using ld_t = long double;
int main() {
ld_t a, b, c, d;
cin >> a >> b >> c>>d;
ld_t A = b * b - 3 * a*c;
ld_t B = b * c - 9 * a*d;
ld_t C = c * c - 3 * b*d;
//由于题目保证有三个实根 所以就不需要判别式了
ld_t T = (2 * A * b - 3 * a * B) / (2 * A * sqrtl(A));
ld_t theta = acosl(T); //盛金公式4中的θ
ld_t x1 = (-b - 2 * sqrtl(A) * cosl(theta / 3)) / (3 * a);
ld_t x2 = (-b + sqrtl(A)*(cosl(theta / 3) + sqrtl(3) * sinl(theta / 3))) / (3 * a);
ld_t x3 = (-b + sqrtl(A)*(cosl(theta / 3) - sqrtl(3) * sinl(theta / 3))) / (3 * a);
//按照顺序输出方程的根
if (x2 > x3) swap(x2, x3);
cout.setf(ios::fixed);
cout << setprecision(2) << x1 << " " << setprecision(2) << x2 << " " << setprecision(2) << x3;
return 0;
}
```