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

计算一元二次方程

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

#include <stdio.h> #include <math.h>

int main() { double a = 0; double b = 0; double c = 0; double x = 0; double x1 = 0; double x2 = 0;

while (scanf("%lf %lf %lf", &a, &b, &c) != EOF)
{
    if (a != 0)
    {
        x = b*b - 4*a*c;
        if (x == 0)
        {
             x1 = -b*1.0/(2*a);
            printf("x1=x2=%.2lf\n", x1);
        }
        else if (x > 0)
        {
            x1 = (-b+sqrt(x))/(2.0*a);
            x2 = (-b-sqrt(x))/(2.0*a);
            if (x1 > x2)
            {
                double tmp = x1;
                x1 = x2;
                x2 = tmp;
            }
            printf("x1=%.2lf;x2=%.2lf\n", x1, x2);
        }
        else
        {
            x1 = -b*1.0/(2*a);
            x2 = sqrt(-x)/(2*a);
            if (a < 0)
                x2 = -x2;
            printf("x1=%.2lf-%.2lfi;x2=%.2lf+%.2lfi\n", x1, x2, x1, x2);
        }
    }
    else
        printf("Not quadratic equation\n");
}
return 0;

}

全部评论

相关推荐

不愿透露姓名的神秘牛友
11-27 10:28
点赞 评论 收藏
分享
10-29 15:38
门头沟学院 Java
榕城小榕树:难道你简历里写了配送路径优化算法?
点赞 评论 收藏
分享
11-18 09:44
Java
小白也想要offer:简历别放洋屁,搞不还还放错了,当然你投外企除外,以上纯属个人观点
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务