题解 | 计算一元二次方程

计算一元二次方程

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

#include <cstdio>
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
int main()
{
    float a,b,c;
    while(scanf("%f %f %f",&a,&b,&c)!=EOF)
    {
        float d=b*b-4*a*c;
    float f=(-b-sqrt(d))/(2*a);
    float g=(-b+sqrt(d))/(2*a);
    if (a==0)
    cout<<"Not quadratic equation";
    if(-b==-0&&sqrt(d)/(2*a)==0) f=g=0;
    if(a>0)
    {
    if(d==0) cout<<fixed<<setprecision(2)<<"x1=x2="<<f<<endl;
    else if(d>0) cout<<fixed<<setprecision(2)<<"x1="<<f<<';'<<"x2="<<g<<endl;
    else cout<<fixed<<setprecision(2)<<"x1="<<-b/(2*a)<<'-'<<sqrt(-d)/(2*a)<<'i'<<';'<<"x2="<<-b/(2*a)<<'+'<<sqrt(-d)/(2*a)<<'i'<<endl;
    }
    if(a<0)
    {
     if(d==0) cout<<fixed<<setprecision(2)<<"x1=x2="<<f<<endl;
    else if(d>0) cout<<fixed<<setprecision(2)<<"x1="<<g<<';'<<"x2="<<f<<endl;
    else 
    cout<<fixed<<setprecision(2)<<"x1="<<-b/(2*a)<<'+'<<sqrt(-d)/(2*a)<<'i'<<';'<<"x2="<<-b/(2*a)<<'-'<<sqrt(-d)/(2*a)<<endl;   
    }
    }
    return 0;
}

全部评论
能运行就好
点赞 回复 分享
发布于 2025-10-20 11:10 四川

相关推荐

烤点老白薯:亲娘嘞🐶💩啊你的简历
点赞 评论 收藏
分享
01-30 22:03
门头沟学院 Java
用微笑面对困难:我滴妈,【俩月】【实习】【主管】仨debuff吃满了,独立设计开发的项目写了绝大占比的运营板块,你独立开发,那维护、问题复盘、日志更新、bug、策划书全是自己整的? 不建议写那么大,可以从小出发更容易
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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