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

计算一元二次方程

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

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

    return 0;
}

全部评论

相关推荐

Hello_WordN:咱就是说,除了生命其他都是小事,希望面试官平安,希望各位平时也多注意安全
点赞 评论 收藏
分享
1 收藏 评论
分享
牛客网
牛客企业服务