top1编程
← 返回题目
题解

求一元二次方程

1 条题解

  • 0
    @ 2026-7-28 22:45:11
    #include<iostream>
    #include<cmath>
    #include<iomanip>
    using namespace std;
    int main()
    {    
        double a,b,c,d,x1,x2;
        cin>>a>>b>>c;
        d=b*b-4*a*c;
        if(d>0.000001){
        	x1=(-b+sqrt(d))/(2*a);
    		x2=(-b-sqrt(d))/(2*a);
    		if(a<0){
    		cout<<"x1=";printf("%0.5lf",x1);cout<<";"<<"x2=";printf("%0.5lf",x2);}
    		else{
    			cout<<"x1=";printf("%0.5lf",x2);cout<<";"<<"x2=";printf("%0.5lf",x1);
    		}
    	}
    	if(d >= -0.000001 && d <= 0.000001){
        	x1=x2=(-b)/(2*a);
        	cout<<"x1=x2=";printf("%0.5lf",x1);
    	}
    		if(d < -0.000001){
        	cout<<"No answer!";
    	}
        return 0;
    }
    
    • 1