题解 | #计算一元二次方程#
计算一元二次方程
https://www.nowcoder.com/practice/7da524bb452441b2af7e64545c38dc26
#include <stdio.h>
#include <math.h>
int main() {
double a, b, c, delta, x1, x2, real, image;
while (scanf("%lf %lf %lf", &a, &b, &c) != EOF) { // 注意 while 处理多个 case
// 64 位输出请用 printf("%lld") to
if (0 == a){
printf("Not quadratic equation\n");
}
else{
delta = b*b - 4*a*c;
if (0 == delta){
x1 = x2 = (-b+sqrt(delta)) / (2*a);
printf("x1=x2=%.2f\n", x1+0);
}
else if (delta > 0){
x1 = (-b-sqrt(delta)) / (2*a);
x2 = (-b+sqrt(delta)) / (2*a);
printf("x1=%.2f;x2=%.2f\n", x1, x2);
}
else{
real = b / (-2*a);
// printf("real= %.2f\n", real);
image = sqrt(-delta) / (2*a);
printf("x1=%.2f-%.2fi;x2=%.2f+%.2fi\n", real+0, image, real+0, image);
}
}
}
return 0;
}
查看11道真题和解析