题解 | #计算一元二次方程#
计算一元二次方程
https://www.nowcoder.com/practice/7da524bb452441b2af7e64545c38dc26
#include <stdio.h> #include<math.h> int main() { float a, b, c; while (scanf("%f%f%f", &a, &b, &c) != EOF) { float der = b * b - 4 * a * c; if (a == 0) printf("Not quadratic equation"); else { if (der == 0) printf("x1=x2=%.2f\n", b / -(2 * a * 1.0) + 0);//必须加+0不然会输出-0 else if (der > 0) printf("x1=%.2f;x2=%.2f\n", (-b - (float)sqrt(der)) / (2 * a * 1.0), (-b + (float)sqrt(der)) / (2 * a * 1.0)); else printf("x1=%.2f-%.2fi;x2=%.2f+%.2fi\n", -b / (2 * a), sqrt(-der) / (2.0 * a), -b / (2.0 * a), sqrt(-der) / (2.0 * a)); } } return 0; }