题解 | #计算一元二次方程#
计算一元二次方程
https://www.nowcoder.com/practice/7da524bb452441b2af7e64545c38dc26
#include <stdio.h> #include<math.h> int main() { float a = 0.0; float b = 0.0; float c = 0.0; float derta = 0.0; while(scanf("%f %f %f",&a,&b,&c)!=EOF) if (a == 0.0) { printf("Not quadratic equation"); } else { derta = b * b - 4*a * c; if (derta == 0) { float r = -b / (2 * a); printf("x1=x2=%.2f\n", r==0?0:r); //这里容易想不到,若不这样写,结果是-0.00,不符合题目 要求 } else if (derta > 0) { float t1 = (-b - sqrt(derta)) / (2 * a); float t2 = (-b + sqrt(derta)) / (2 * a); printf("x1=%.2f;x2=%.2f\n", t1, t2); } else { float shibu = -b / (2 * a); float xubu = sqrt(-derta) / (2 * a); printf("x1=%.2f-%.2fi;x2=%.2f+%.2fi\n", shibu, xubu, shibu, xubu); } } return 0; }