60分求助
查看原帖
60分求助
1155466
iamsbs楼主2024/5/28 21:22
#include<bits/stdc++.h>
using namespace std;
int t,m,sj,a,b,c,x;
int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin>>t>>m;
	for(int i=1;i<=t;i++){
		cin>>a>>b>>c;
		sj=b*b-4*a*c;
		if(a<0){
			a=-a;
			b=-b;
			c=-c;
		}if(sj<0){
			cout<<"NO\n";
			continue;
		}if(sqrt(sj)*sqrt(sj)==sj){
			int t=max((-b+sqrt(sj)),(-b-sqrt(sj)));
			int tmp=abs(__gcd(t,2*a));
			if(2*a==tmp){
				cout<<t/tmp<<'\n';
			}else{
				cout<<t/tmp<<'/'<<2*a/tmp<<'\n';
			}continue;
		}
		int t=abs(__gcd(-b,2*a));
		int q1=(-b)/t;
		int q2=(2*a)/t;
		if(q1!=0){
			if(q2==1){
				cout<<q1;
			}else{
				cout<<q1<<'/'<<q2;
			}cout<<'+';
		}
		int r=sj;
		q2=1;i=2;
		while(sj>1){
			int cnt=0;
			while(sj%i==0){
				sj/=i;
				if(cnt>0){
					r/=i;
					q2*=i;
				}cnt++;
			}i++;
		}a=2*a;
		t=abs(__gcd(q2,a));
		q2/=t;
		a/=t;
		if(q2==1&&a==1){
			cout<<"sqrt("<<r<<')';
		}else if(q2%a==0){
			cout<<q2/a<<'*'<<"sqrt("<<r<<')';
		}else if(a%q2==0){
			cout<<"sqrt("<<r<<')'<<'/'<<a/q2;
		}else{
			cout<<q2<<'*'<<"sqrt("<<r<<')'<<'/'<<a;
		}cout<<'\n';
	}
	return 0;
}
2024/5/28 21:22
加载中...