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

计算一元二次方程

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

include<stdio.h>

include<math.h>

int main()
{
double a, b, c;
while (~scanf("%lf%lf%lf", &a, &b, &c))
{
double x1, x2;
double date = b * b - 4 * a*c;
if (a != 0)
{

        if (b*b - 4 * a*c < 0)//我知道问题所在了,此时根号下是负数
        {     //这里的计算有问题        x1    -nan(ind)??
        x1 = (-b) / (2 * a);
        x2 = (-b) / (2 * a);printf("x1=%.2lf-%.2lfi;x2=%.2lf+%.2lfi\n", (-b) / (2 * a), sqrt(-date) / (2 * a),
     (-b) / (2 * a), sqrt(-date) / (2 * a));//关键在于分开写
        //printf("x1=%.2lf+%lfi;x2=%.2lf-%lfi\n", x1, sqrt(-(b*b - 4 * a*c)) / (2 * a), x2, sqrt(-(b*b - 4 * a*c)) / (2 * a));
        }
        else if (b*b - 4 * a*c > 0)
        {
            x1 = (-b - sqrt(b*b - 4 * a*c)) / (2 * a);
            x2 = (-b + sqrt(b*b - 4 * a*c)) / (2 * a);
            printf("x1=%.2lf;x2=%.2lf\n", x1, x2);
        }
        else if ((b*b - 4 * a*c) == 0)
        {
            x1 = x2 = -(b/(2*a) );
            printf("x1=x2=%.2lf\n", x1);
        }
    }
    else
        printf("Not quadratic equation\n");
}return 0;
全部评论

相关推荐

投递字节跳动等公司10个岗位
点赞 评论 收藏
转发
点赞 收藏 评论
分享
牛客网
牛客企业服务