题解 | 计算一元二次方程

计算一元二次方程

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

#include <iostream>
#include<cmath>
using namespace std;

int main() {
    double a, b, c;
    while (cin >> a >> b >> c) {
        if (a == 0) cout << "Not quadratic equation" << endl;
        if (a != 0) {
            double derta = b * b - 4 * a * c;
            double x1, x2;
            if (derta == 0) {
                x1 = (-b + sqrt(derta)) / (2 * a);
                x2 = (-b - sqrt(derta)) / (2 * a);
                if (x1 == 0.00) {
                    printf("x1=x2=%.2lf", abs(x1));
                } else printf("x1=x2=%.2lf", x1);
            } else if (derta > 0) {
                x1 = (-b - sqrt(derta)) / (2 * a);
                x2 = (-b + sqrt(derta)) / (2 * a);
                printf("x1=%.2lf;x2=%.2lf", x1, x2);
            } else {
                double s, x;
                s = -b / (2 * a);
                x = (sqrt(-derta)) / (2 * a);
                printf("x1=%.2lf-%.2lfi;x2=%.2lf+%.2lfi", s, x, s, x);

            }
        }
        cout<<endl;
    }

}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

起名字真难233:人家只有找猴子的预算,来个齐天大圣他们驾驭不住呀😂😂
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务