题解 | #计算一元二次方程#
计算一元二次方程
https://www.nowcoder.com/practice/7da524bb452441b2af7e64545c38dc26
#include <stdio.h> #include <math.h> int main() { double a, b, c; double x1 = 0.00, x2 = 0.00; //方程的根 double x =0.00,y=0.00;//x表示实部,y表示虚部 while (scanf("%lf %lf %lf", &a, &b, &c) != EOF) { if (a == 0) { printf("Not quadratic equation\n"); } else { if (b * b - 4 * a * c == 0) { //等于零单实根 if (b==0&&c==0) { printf("x1=x2=0.00\n"); } else { x1 = -1.0 * b / (2.0 * a); printf("x1=x2=%.2lf\n", x1); } } else if (b * b - 4 * a * c > 0) { //大于零双实根 x1 = (-b - sqrt(b * b - 4 * a * c)) / 2.0 / a; x2 = (-b + sqrt(b * b - 4 * a * c)) / 2.0 / a; printf("x1=%.2lf;x2=%.2lf\n", x1, x2); } else { //小于零虚根 x = -b/(2.0*a); y = sqrt(4 * a * c - b * b)/(2.0*a); printf("x1=%.2lf-%.2lfi;x2=%.2lf+%.2lfi\n",x,y,x,y ); } } } return 0; }