题解 | #计算一元二次方程#

计算一元二次方程

https://www.nowcoder.com/practice/7da524bb452441b2af7e64545c38dc26

#include<stdio.h>
#include<math.h>
int main() {
    double a = 0;
    double b = 0;
    double c = 0;
    while (scanf("%lf%lf%lf", &a, &b, &c) != EOF) {
        if (a == 0)
            printf("Not quadratic equation\n");
        else {
            double delta = pow(b, 2) - 4 * a * c;
            double x1 = (-b - sqrt(delta)) / (2 * a);
            double x2 = (-b + sqrt(delta)) / (2 * a);
            if (delta == 0) {
                double x = -b / (2 * a);
                if (x1 == 0)
                    printf("x1=x2=0.00\n");
                else
                    printf("x1=x2=%.2f\n", x1);;
            } else if (delta > 0) {
                printf("x1=%.2f;x2=%.2f\n", x1, x2);
            } else {
                double A = -b / (2 * a);
                double B = sqrt(-delta) / (2 * a);
                printf("x1=%.2f-%.2fi;x2=%.2f+%.2fi\n", A, B, A, B);
            }
        }
    }


    return 0;
}

全部评论

相关推荐

点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务