题解 | #计算一元二次方程#
计算一元二次方程
https://www.nowcoder.com/practice/7da524bb452441b2af7e64545c38dc26
#include <stdio.h> #include <math.h> int main() { float a, b, c; float n = 0.0; while (scanf("%f %f %f", &a, &b, &c) != EOF) { if (a == 0) { printf("Not quadratic equation\n"); } else { n = b * b - 4 * a * c; if (n == 0) { if ((-b / 2 * a) == -0.00) { printf("x1=x2=0.00\n"); //浮点型的-0.0 != 0.0整型的-0 == 0 } else { printf("x1=x2=%.2f\n", -b / (2 * a));//同时为分母的要打括号 } } else if (n > 0) { printf("x1=%.2f;x2=%.2f\n", (-b - sqrt(n)) / (2 * a), (-b + sqrt(n)) / (2 * a)); } else { printf("x1=%.2f-%.2fi;x2=%.2f+%.2fi\n", -b / (2 * a), sqrt(-n) / (2 * a), -b / (2 * a), sqrt(-n) / (2 * a)); } } } return 0; }