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