题解 | #计算一元二次方程#
计算一元二次方程
https://www.nowcoder.com/practice/7da524bb452441b2af7e64545c38dc26
#include <stdio.h> #include <math.h> int main() { double a,b,c,del; while(scanf("%lf%lf%lf",&a,&b,&c)!=EOF) { if(a==0) printf("Not quadratic equation"); else { del=b*b-4*a*c; if(del==0) { float mp = -b + sqrt(del); if(mp == 0) { printf("x1=x2=%.2f\n",mp); }else { printf("x1=x2=%.2f\n",(-b+sqrt(del))/2.0/a); } } else if(del>0) { printf("x1=%.2lf;x2=%.2lf\n",(-b-sqrt(del))/2/a,(-b+sqrt(del))/2/a); } else { printf("x1=%.2lf-%.2lfi;x2=%.2lf+%.2lfi\n",-b/2/a,sqrt(-del)/2/a,-b/2/a,sqrt(-del)/2/a); } } } return 0; }
当delta=0时为了防止屏幕输出x1=x2=0.00(原因在-b的-号上),应该对b进行分类讨论