nikoEzGami


私信TA

用户名:qqzz5231181

访问量:3942

签 名:

等  级
排  名 6406
经  验 1364
参赛次数 0
文章发表 21
年  龄 0
在职情况 学生
学  校 湖北工业大学
专  业

  自我简介:

湖北工业大学计算机学院

解题思路:

 一元二次方程

标准形式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 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换

万能编程问答  

代码解释器

代码纠错

SQL生成与解释

  评论区