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