0pts 求调
查看原帖
0pts 求调
1288371
abc123_cn楼主2025/6/29 21:49

提交记录:https://www.luogu.com.cn/record/221810808

#include <bits/stdc++.h>
using namespace std;

int t, m, a, b, c;

bool check(double x)
{
	double num = sqrt(x);
	return pow((long double) num, 2) == x;
}

int Find(int num)
{
	int tot;
	for (int i = 1; i <= num; i ++)
		if (num % i == 0 && check(i)) tot = sqrt(i);
	return tot;
}

int gcd(int a, int b)
{
    if (b == 0) return a;
    else return gcd(b, a % b);
}

void f(int a, int b, int c)
{
    int delta = b * b - 4 * a * c;
    if (delta < 0)
    {
        cout << "NO" << endl;
        return;
    }
    else
    {
        double num = sqrt(delta), root = max((-b + num) / (2 * a), (-b - num) / (2 * a));
        if (floor(root) == root)
        {
            cout << root << endl;
            return;
        }
        else if (check(delta))
        {
            int g = gcd(max(-b + num, -b - num), 2 * a);
            if ((max(-b + num, -b - num) < 0 && 2 * a > 0) || (max(-b + num, -b - num) > 0 && 2 * a < 0)) g = -g;
            cout << (-b + num) / g << "/" << (2 * a) / g << endl; 
            return;
        }
        else
        {
			int q2 = Find(delta), r = delta / (q2 * q2);
        	if (-b % (2 * a) == 0)
			{
				int q1 = -b / (2 * a);
				if (q1 != 0) cout << q1 << "+"; 
			} 
        	else
        	{
	            int g1 = gcd(-b, 2 * a);
	            if ((-b < 0 && 2 * a > 0) || (-b > 0 && 2 * a < 0)) g1 = -g1;
	            cout << -b / g1 << "/" << (2 * a) / g1 << "+";
	            
			}
            if (q2 > 1)
		        if (q2 % (2 * a) == 0)
				{
					q2 = q2 / (2 * a);
					if (q2 == 1) cout << "sqrt(" << r << ")" << endl;
					else cout << q2 << "*sqrt(" << r << ")" << endl;
				}
				else 
				{
					int g2 = gcd(q2, 2 * a);
					if ((g2 < 0 && 2 * a > 0) || (g2 > 0 && 2 * a < 0)) g2 = -g2;
					if (q2 / g2 == 1) cout << "sqrt(" << r << ")/" << (2 * a / g2) << endl;
					else cout << q2 / g2 << "*sqrt(" << r << ")/" << (2 * a / g2) << endl;
				} 	            	
			else if (q2 == 1) cout << "sqrt(" << r << ")/" << 2 * a << endl;
			return;
        }
    }
}

int main()
{
    cin >> t >> m;
    for (int i = 1; i <= t; i ++)
    {
        cin >> a >> b >> c;
        f(a, b, c);
    }
}

蒟蒻向各位大佬请教

2025/6/29 21:49
加载中...