J组T3代码求调
  • 板块学术版
  • 楼主CtrlEEE
  • 当前回复1
  • 已保存回复1
  • 发布时间2023/10/27 14:20
  • 上次更新2023/11/2 11:14:57
查看原帖
J组T3代码求调
938852
CtrlEEE楼主2023/10/27 14:20

#2 #9WA

#4 #10 TLE 求调

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

int t, m;
int a, b, c;

int tmp[2] = {0};

bool is_R(int num) {
	float a, b;
	a = (double)((int) sqrt((double) num));
	b = sqrt((double) num);
	if (a == b)
		return true;
	else
		return false;
}

bool RS(int num) { //根式化简
	for (int i = num - 1; i >= 4; i--) {
		if (is_R(i) && num % i == 0) {
			tmp[0] = (int) sqrt((double) i);
			tmp[1] = num / i;
			return true;
		}
	}
	return false;
}

int main() {
	ios::sync_with_stdio(false);
	cin >> t >> m;
	//freopen("t3.ans", "w", stdout);
	while (t--) {
		cin >> a >> b >> c;
		int delta = b * b - 4 * a * c;
		//NO
		if (delta < 0)
			cout << "NO" << endl;

		//有理数
		else if (is_R(delta)) {
			int zi;
			if (a > 0)
				zi = -b + ((int) sqrt((double)delta));
			else
				zi = -b - ((int) sqrt((double)delta));
			int mu = 2 * a;
			if (mu < 0) {
				zi = -zi;
				mu = -mu;
			}
			if (zi == 0) {
				cout << 0 << endl;
			} else if (mu == 1) {
				cout << zi << endl;
			} else {
				int huajian = __gcd(zi, mu);
				zi /= huajian;
				mu /= huajian;
				if (mu < 0) {
					zi = -zi;
					mu = -mu;
				}
				if (mu == 1) {
					cout << zi << endl;
				} else
					cout << zi << "/" << mu << endl;;
			}
		}

		//无理数
		else {
			if (b == 0) { //q1为0
				if (RS(delta)) {
					if (2 * a == tmp[0]) {
						cout << "sqrt(" << tmp[1] << ")" << endl;
					} else if (tmp[0] % (2 * a) == 0) {
						cout << tmp[0] / (2 * a) << "*sqrt(" << tmp[1] << ")" << endl;
					} else {
						int zi, mu;
						int huajian = __gcd(tmp[0], 2 * a);
						zi = tmp[0] / huajian;
						mu = 2 * a / huajian;
						if (mu < 0) {
							zi = -zi;
							mu = -mu;
						}
						if (zi == 1)
							cout << "sqrt(" << tmp[1] << ")" << " / " << mu << endl;
						else
							cout << zi << "*sqrt(" << tmp[1] << ")" << "/" << mu << endl;

					}
				} else if (a > 0)
					cout << "sqrt(" << delta << ")" << "/" << 2 * a << endl;
				else
					cout << "-sqrt(" << delta << ")" << "/" << -2 * a << endl;


			} else { //q1!=0
				int first_zi = -b;
				int first_mu = 2 * a;
				int first_huajian = __gcd(first_zi, first_mu);
				first_zi /= first_huajian;
				first_mu /= first_huajian;
				if (first_mu < 0) {
					first_zi = -first_zi;
					first_mu = -first_mu;
				}
				if (first_mu == 1) {
					cout << 1 << "+";
				} else
					cout << first_zi << "/" << first_mu << "+";

				if (RS(delta)) {
					//cout << endl << tmp[0] << "&&" << tmp[1] << endl;
					if (2 * a == tmp[0]) {
						cout << "sqrt(" << tmp[1] << ")" << endl;
					} else if (tmp[0] % (2 * a) == 0) {
						cout << tmp[0] / (2 * a) << "*sqrt(" << tmp[1] << ")" << endl;
					} else {
						int zi, mu;
						int huajian = __gcd(tmp[0], 2 * a);
						zi = tmp[0] / huajian;
						mu = 2 * a / huajian;
						if (mu < 0) {
							zi = -zi;
							mu = -mu;
						}
						if (zi == 1)
							cout << "sqrt(" << tmp[1] << ")" << "/" << mu << endl;
						else
							cout << zi << "*sqrt(" << tmp[1] << ")" << "/" << mu << endl;

					}
				} else if (a > 0)
					cout << "sqrt(" << delta << ")" << "/" << 2 * a << endl;
				else
					cout << "-sqrt(" << delta << ")" << "/" << -2 * a << endl;
			}
		}
	}
	return 0;
}
2023/10/27 14:20
加载中...