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