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

计算一元二次方程

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

#include <stdio.h>
#include<math.h>
int main() {
    double a = 0.0;
    double b = 0.0;
    double c = 0.0;
    while (scanf("%lf %lf %lf", &a, &b,&c) != EOF) 
    { 
        if(a == 0)
        {
            printf("Not quadratic equation\n");
        }
        else 
        {
             double d = b*b - 4*a*c;
            if(d == 0.0)
            {
                if (b == 0.0)
                {
                     printf("x1=x2=0.00\n");
                }
                else
                {
                    printf("x1=x2=%.2lf\n", (-b) / (2 * a));
                }
            }
            else if(d > 0.0)
            {
               double x1 = (-b-sqrt(d))/(2*a);
               double x2 = (-b+sqrt(d))/(2*a);
                printf("x1=%.2lf;x2=%.2lf\n",x1,x2);
            }
            else 
            {
                double real = (-b) / (2 * a);
                double image = sqrt(-d) / (2 * a);
                printf("x1=%.2lf-%.2lfi;x2=%.2lf+%.2lfi\n", real, image, real, image);
            }
        }
    }
    return 0;
}

全部评论

相关推荐

风的叶脉:不知道但我想要鞭打你( '-' )ノ)`-' ) 加油
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务