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