解题思路:
一元二次方程
标准形式ax²+bx+c=0(a≠0)
求根公式* x=[-b±√(b²-4ac)]/2a
判别式delta=b²-4ac
如果delta<0,那么方程有2个共轭复数根
求根公式依然适用,引入定义i^2=-1
举例x=[-2±√(-20)]/2=-1±i√5
尝试#include<complex>求解未果,如果有友友这种方法做出来请直接评论留言,谢谢~
注意事项:
暂时不明
参考代码:
#include<iostream>
#include<iomanip>
#include<math.h>
using namespace std;
double solution_1(double,double,double);
double solution_2(double,double);
void solution_3(double,double,double);
int main()
{
double a,b,c;
cin>>a>>b>>c;
double delta=b*b-4*a*c;
float x_1,x_2;
if (delta>0){
x_1,x_2=solution_1(a,b,delta);
cout<<fixed<<setprecision(3)<<"x1="<<x_1<<' '<<"x2="<<x_2<<endl;
}
else if(delta==0){
x_1=solution_2(a,b);
x_2=x_1;
cout<<fixed<<setprecision(3)<<"x1="<<x_1<<' '<<"x2="<<x_2<<endl;
}
else if (delta<0) solution_3(a,b,delta);
return 0;
}
double solution_1(double a,double b,double delta)
{
float x_1,x_2;
x_1=(sqrt(delta)-b)/(2*a);
x_2=(-1)*(sqrt(delta)+b)/(2*a);
return x_1,x_2;
}
double solution_2(double a,double b)
{
float x_1;
x_1=(-b)/(2*a);
return x_1;
}
void solution_3(double a,double b,double delta)
{
double real_part = -b/(2*a);
double imaginary_part =sqrt(-delta)/(2*a);
cout<<fixed<<setprecision(3)<<"x1="<<real_part<<'+'<<imaginary_part<<'i'<<" x2="<<real_part<<'-'<<imaginary_part<<'i'<<endl;
}
0.0分
2 人评分