题解 | #计算一元二次方程#
计算一元二次方程
https://www.nowcoder.com/practice/7da524bb452441b2af7e64545c38dc26
#include <stdio.h> #include <math.h> int main() { double a = 0; double b = 0; double c = 0; while (3 == scanf("%lf %lf %lf", &a, &b, &c)) { if (a != 0) { double dif = b * b - 4 * a * c; if (0 == dif) { printf("x1=x2=%.2lf\n", ((-b / (2 * a) != 0) ? (-b / (2 * a)): 0)); } else if (dif > 0) { printf("x1=%.2lf;x2=%.2lf\n", (-sqrt(dif) - b) / (2 * a), (sqrt(dif) - b) / (2 * a)); } else { printf("x1=%.2lf-%.2lfi;x2=%.2lf+%.2lfi\n",(-b)/ (2 * a),sqrt(-dif) / (2 * a), (-b) / (2 * a), (sqrt(-dif) / (2 * a))); } } else { printf("Not quadratic equation\n"); } } return 0; }