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

计算一元二次方程

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

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

int main()
{
    float a,b,c;
    float z;
    while(scanf("%f %f %f\n",&a,&b,&c)!=EOF)
    {
        if(a==0)
        printf("Not quadratic equation\n");
        else
        {
            z=(b*b)-(4*a*c);
            if(z==0)
            {
                if(b==0&&c==0)
                printf("x1=x2=0.00");//防止出现这种情况时输出结果为-0.00
                else
                {float x=-b/(2*a);
                printf("x1=x2=%0.2f\n",x);}
            }
            else if(z>0)
            {
                float x1= (-b / (2*a))-sqrt(z/(4*a*a));
                float x2=(-b / (2*a))+sqrt(z/(4*a*a));
                printf("x1=%0.2f;x2=%0.2f\n",x1,x2);
            }
            else 
            {
                float x= -b / (2*a);
                float x1=sqrt(-z) / (2*a);
                printf("x1=%0.2f-%0.2fi;x2=%0.2f+%0.2fi",x,x1,x,x1);
            }
        }
    }
    return 0;
}

全部评论

相关推荐

02-25 19:38
门头沟学院 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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