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