Description |
n组测试数据。 每组测试数据输入二次项,一次项,常数项系数a(a不为0),b,c,输出两个根。 |
Input |
第一行输入测试数据组数n。 以下n行,每行输入三个数a,b,c,数据之间空格分隔。 |
Output |
每组输出测试数据占一行,输出两个根,空格分隔。 |
Sample Input |
4 1 0 1 4 4 1 1 -1 4 1 4 1 |
Sample Output |
1.00i -1.00i -0.50 -0.50 0.50+1.94i 0.50-1.94i -0.27 -3.73 |
#include<iostream>
#include<iomanip>
#include<math.h>
using namespace std;
int main() {
int n;
cin >> n;
while (n--) {
double a, b, c;
cin >> a >> b >> c;
double x1, x2;
double y1;
if (b * b - 4 * a * c >= 0) {
x1 = (-b + sqrt(b * b - 4 * a * c)) / (2 * a);
x2 = (-b - sqrt(b * b - 4 * a * c)) / (2 * a);
cout << fixed << setprecision(2) << x1 << ' ' << x2 << endl;
}
if (b * b - 4 * a * c < 0) {
y1 = (-b) / (2 * a);
x1 = (sqrt(-(b * b - 4 * a * c))) / (2 * a);
x2 = (- sqrt(-(b * b - 4 * a * c))) / (2 * a);
if (y1 != 0) {
cout << fixed << setprecision(2) << y1 << "+" << x1 << "i ";
cout << fixed << setprecision(2) << y1 << x2 << "i" << endl;
}
else {
cout << fixed << setprecision(2) << x1 << "i ";
cout << fixed << setprecision(2) << x2 << "i" << endl;
}
}
}
return 0;
}