题解 | 计算一元二次方程
计算一元二次方程
https://www.nowcoder.com/practice/7da524bb452441b2af7e64545c38dc26
#include <iostream> #include<cmath> using namespace std; int main() { double a, b, c; while (cin >> a >> b >> c) { if (a == 0) cout << "Not quadratic equation" << endl; if (a != 0) { double derta = b * b - 4 * a * c; double x1, x2; if (derta == 0) { x1 = (-b + sqrt(derta)) / (2 * a); x2 = (-b - sqrt(derta)) / (2 * a); if (x1 == 0.00) { printf("x1=x2=%.2lf", abs(x1)); } else printf("x1=x2=%.2lf", x1); } else if (derta > 0) { x1 = (-b - sqrt(derta)) / (2 * a); x2 = (-b + sqrt(derta)) / (2 * a); printf("x1=%.2lf;x2=%.2lf", x1, x2); } else { double s, x; s = -b / (2 * a); x = (sqrt(-derta)) / (2 * a); printf("x1=%.2lf-%.2lfi;x2=%.2lf+%.2lfi", s, x, s, x); } } cout<<endl; } } // 64 位输出请用 printf("%lld")