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

计算一元二次方程

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

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

int main() {
    float a,b,c,der,x,x1,x2;
    while((scanf("%f %f %f",&a,&b,&c))!= EOF)
    {
        der = b*b-4*a*c;
        if(a==0.0)
        {
            printf("Not quadratic equation");
        }
        else 
        {
            if(der == 0)
            {
                x1 = (-b)/(2*a);
                x2 = (-b)/(2*a);
                if(x1 == -0.0)
                {
                    x1 = 0.0;
                    printf("x1=x2=%.2f\n",x1);
                }
                else 
                {
                    printf("x1=x2=%.2f\n",x1);
                }
            }
            else if(der > 0)
            {
                x1 = (-b+sqrtf(der))/(2*a);
                x2 = (-b-sqrtf(der))/(2*a);
                if(x1<=x2)
                {
                    printf("x1=%.2f;x2=%.2f\n",x1,x2);
                }
                else 
                {
                    x = x1;
                    x1 = x2;
                    x2 = x;
                    printf("x1=%.2f;x2=%.2f\n",x1,x2);
                }
            }
            else 
            {
                x = sqrtf(4*a*c-b*b)/(2*a);
                x1 = (-b)/(2*a);
                x2 = (-b)/(2*a);
                printf("x1=%.2f-%.2fi;x2=%.2f+%.2fi\n",x1,x,x2,x);
            }
        }




    }



    return 0;
}

全部评论

相关推荐

07-02 13:52
武汉大学 golang
骗你的不露头也秒
牛客87776816...:😃查看图片
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务