题解 | #计算一元二次方程#
计算一元二次方程
https://www.nowcoder.com/practice/7da524bb452441b2af7e64545c38dc26
#include<stdio.h>
#include<math.h>
int main() {
float a = 0, b = 0, c = 0;
float x1, x2;
while (scanf("%f %f %f", &a, &b, &c) != EOF) {
// float d=b*b-4*a*c;
if (a == 0)
printf("Not quadratic equation");
else {
float disc = b * b - 4 * a * c;
if (disc == 0) {
float s = -b + sqrt(disc);
if (s == 0) {
printf("x1=x2=%.2f\n", s);
} else {
printf("x1=x2=%.2f\n", (-b) / 2.0 / a);
}
} else if (disc < 0) {
printf("x1=%.2f-%.2fi;x2=%.2f+%.2fi\n", -b / 2 / a, sqrt(-disc) / 2 / a,
-b / 2 / a, sqrt(-disc) / 2 / a);
}
else
printf("x1=%.2f;x2=%.2f\n", (-b - sqrt(disc)) / 2.0 / a, (-b + sqrt(disc)) / 2.0 / a);
}
}
return 0;
}
c语言刷题 文章被收录于专栏
c语言刷题题目