社区讨论

70pts 求调

P9750[CSP-J 2023] 一元二次方程参与者 2已保存回复 1

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
1 条
当前快照
1 份
快照标识符
@mhjh5351
此快照首次捕获于
2025/11/04 02:29
4 个月前
此快照最后确认于
2025/11/04 02:29
4 个月前
查看原帖
70pts 紧急求调,代码:
CPP
#include <bits/stdc++.h>
using namespace std;

int e, m, a, b, c;

int gcd(int x, int y) {
    return y > 0 ? gcd(y, x % y) : x;
}

void p1(int x, int y) {
    if (x == 0) {
        cout << 0;
    }
    else {
        if (y < 0) {
            x = -x;
            y = -y;
        }
        int t = gcd(x, y);
        x /= t;
        y /= t;
        cout << x;
        if (y != 1)
            cout << '/' << y;
    }
}

int find(int x) {
    int ans = 0;
    for (int i = 1; i * i <= x; i++)
        if (x % (i * i) == 0)
            ans = i;
    return ans;
}

void p2(int x, int y) {
    int q = find(x), r = x / (q * q);
    int t = gcd(q, y);
    q /= t;
    y /= t;
    if (q != 1)
        cout << q << "*sqrt(" << r << ")";
    else
        cout << "sqrt(" << r << ")";
    if (y != 1)
        cout << "/" << y;
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin >> e >> m;
    while (e--) {
        cin >> a >> b >> c;
        int det = b * b - 4 * a * c;
        if (det < 0) {
            cout << "NO" << endl;
            continue;
        }
        int t = sqrt(det);
        if (t * t == det) {
            if (a > 0)
                p1(-b + t, 2 * a);
            else
                p1(-b - t, 2 * a);
        }
        else {
            if (b != 0) {
                p1(-b, 2 * a);
                cout << '+';
            }
            if (a < 0)
                p2(det, -2 * a);
            else
                p2(det, 2 * a);
        }
        cout << endl;

    }
    return 0;
}

回复

1 条回复,欢迎继续交流。

正在加载回复...