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