P9750 [CSP-J 2023] 一元二次方程

 

 

题解:大模拟

∆= b²-4ac.

1.∆<0,无解,输出"NO"

2.∆≥0 x1=(-b/2a)+(√∆/2a),x1=(-b/2a)-(√∆/2a)

当a<0时,最大解为x2

当a>0时,最大解为x1

……………………………………………………………………

【AC代码】

#include<bits/stdc++.h> 
using namespace std;
int T,m,a,b,c,d,k,t;
int gcd(int a, int b)
{
    if(a == 0) return b;
    else gcd(b % a, a);
}

int main()
{
    int T, m, a, b, c, d, k, t;
    cin >> T >> m;
    for(int i = 1; i <= T; i++)
    {
        cin >> a >> b >> c;
        if(a < 0)
            a =- a, b =- b, c =- c;
        d = b * b - 4 * a * c, k = 1;
        if(d < 0)
        {
            cout << "NO" << endl;
            break;
        }
        for(int i = 2; i * i <= d; i++)
            while(d % (i * i) == 0)
                k *= i, d /= (i * i);
        if(d==0||d==1)
        {
            t = abs(gcd(2 * a, -b + k * d));
            cout << (-b + k * d) / t;
            if(2 * a / t != 1)
                cout << '/' << 2 * a / t;
            cout << endl;
            break;
        }
        t = abs(gcd(-b, 2 * a));
        if(-b / t == 0)
            goto g;
        cout << -b / t;
        if(2 * a / t != 1)
            cout << '/' << 2 * a / t;
        cout << '+';
        g:
        t = abs(gcd(k, 2 * a));
        if(k / t != 1)
            cout << k / t << '*';
        cout << "sqrt(" << d << ')';
        if(2 * a / t != 1)
            cout << '/' << 2 * a / t;
        cout << endl;
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值