注册 登录
编程论坛 C语言论坛

求一元二次方程,为什么错

Jason_ 发布于 2021-01-31 15:13, 1608 次点击
程序代码:
#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
int main(){
    double a,b,c,d,x1,x2;
    cin>>a>>b>>c;
    x1=(-b+sqrt(b*b-4*a*c))/(2*a);
    x2=(-b-sqrt(b*b-4*a*c))/(2*a);
    d=b*b-4*a*c;
    if(d<0){
        cout<<"No answer!";
    }
    else{
        if(x1==x2){
        printf("x1=x2=%.5f",x1);
    }
        else{
            if(x1>x2){
                printf("x2=%.5f;x1=%.5f",x2,x1);
            }
            if(x1<x2){
                printf("x1=%.5f;x2=%.5f",x1,x2);
            }
        }
    }

   
    return 0;
}

题目链接:http://ybt.
2 回复
#2
Jason_2021-01-31 15:14
错了3个数据点_(:з」∠)_
#3
rjsp2021-02-01 09:01
先确保 b*b-4*a*c >= 0,再 sqrt(b*b-4*a*c)
1