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

计算一元二次方程

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

#include<iomanip>
#include<cmath>
using namespace std;
int main()
{
    float a,b,c;
    float d,e;
    while(cin>>a>>b>>c)
    {
        e=-b/(2*a);
        d=(b*b)-(4*a*c);
        if(a==0)
        cout<<"Not quadratic equation"<<endl;
      else if(a!=0)
      {
        if(d==0)
        {
            cout << fixed << setprecision(2);
            cout<<"x1=x2="<<e<<endl;
        }
        else if(d>0)
        {
            cout << fixed << setprecision(2);
            cout<<"x1="<<e-sqrt(d)/(2*a)<<";"<<"x2="<<e+sqrt(d)/(2*a)<<endl;
        }
        else
        {
            cout << fixed << setprecision(2);
            cout<<"x1="<<e<<"-"<<sqrt(fabs(d))/(2*a)<<"i"<<";"<<"x2="<<e<<"+"<<sqrt(fabs(d))/(2*a)<<"i"<<endl;
        }
      }
    }
    return 0;
}

sqrt里面必须是正数derta小于零时:-1开方=i,里面按正数算

全部评论
。。。。。这能过?
点赞 回复 分享
发布于 2022-04-03 08:49
代码报错了,,,,
点赞 回复 分享
发布于 2021-11-17 19:57

相关推荐

点赞 评论 收藏
分享
下个早班:秒挂就是不缺人
点赞 评论 收藏
分享
头顶尖尖的程序员:我是26届的不太懂,25届不应该是找的正式工作吗?为什么还在找实习?大四还实习的话是为了能转正的的岗位吗
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务