P9750大模拟10分求条,卡了好几个月了 大佬帮忙看看吧
  • 板块灌水区
  • 楼主Lian_zy
  • 当前回复3
  • 已保存回复3
  • 发布时间2024/4/7 17:15
  • 上次更新2024/4/7 19:46:35
查看原帖
P9750大模拟10分求条,卡了好几个月了 大佬帮忙看看吧
923248
Lian_zy楼主2024/4/7 17:15

rt,题目链接

代码

#include <cmath>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

int n, m, a, b, c, p, q, r, qq, sq, gc, delta;
int gcd(int x, int y) {
	if (y) return gcd(y, x % y);
	return x;
} 
int main() {
	scanf("%d %d", &n, &m);
	while (n--) {
		scanf("%d %d %d", &a, &b, &c);
		delta = b * b - (a * c << 2);
		if (delta < 0) {
			puts("NO");
			continue;
		}
		sq = (int)sqrt(delta);
		if (sq * sq == delta) {
			p = -b;
			q = a << 1;
			p += sq;
			gc = gcd(p, q);
			p /= gc;
			q /= gc;
			if (q < 0) {
				p = -p;
				q = -q;
			}
			if (q == 1 && p) printf("%d\n", p);
			else if (p) printf("%d/%d\n", p, q);
			else puts("0");
			continue;
		}
		p = -b;
		q = a << 1;
		gc = gcd(p, q);
		p /= gc;
		q /= gc;
		if (q < 0) {
			p = -p;
			q = -q;
		}
		if (q == 1 && p) printf("%d+", p);
		else if (p) printf("%d/%d+", p, q);
		for (int i = 1; i * i <= delta; i++) {
			if (delta % (i * i)) continue;
			qq = i;
		}
		r = delta / qq / qq;
		q = abs(a << 1);
		gc = gcd(qq, q);
		qq /= gc;
		q /= gc;
		if (qq == 1) printf("sqrt(%d)", r);
		else printf("%d*sqrt(%d)", qq, r);
		if (q == 1) putchar(10);
		else printf("/%d\n", q);
	}
	return 0;
}
2024/4/7 17:15
加载中...