10分求调
查看原帖
10分求调
1284984
huafan楼主2025/1/18 20:18
# include <bits/stdc++.h>
using namespace std;
int t,m;
int getop(int x){return x<0?-1:1;}
int gcd(int x,int y){
	x=getop(x)*x,y=getop(y)*y;
	if(x%y==0) return y;
	return gcd(y,x%y);
}
void out(int b,int a,char op){
	if(a==1&&b) cout<<b<<op;
	else if(b%a){int gd=gcd(b,a);
		cout<<getop(a)*b/gd<<'/'<<getop(a)*a/gd<<op;
	}
	else if(b) cout<<b/a<<op;
}
int main(){
	cin>>t>>m;
	for(int i=0;i<t;i++){
		int a,b,c;
		scanf("%d%d%d",&a,&b,&c);
		int deta=b*b-4*a*c;
		if(deta<0) cout<<"NO"<<endl;
		else{
			int sq=0;
			for(int j=1;j*j<=deta;j++) if(deta%(j*j)==0) sq=j;
			if(-b+sq==0) cout<<0<<endl;
			else if(sq*sq==deta) out(-b+sq,2*a,'\n');
			else if(deta==0) out(-b,2*a,'\n');
			else{
				out(-b,a*2,'+');
				if(deta){
					if(sq==1&&deta!=1) cout<<"sqrt("<<deta<<")/"<<2*a<<endl;
					else if(sq*sq==deta)out(sq,2*a,'\n');
					else{
						int gd=gcd(sq,2*a);
						if(sq==gd&&2*a==gd)cout<<"sqrt("<<deta/(sq*sq)<<")";
						else if(sq==gd)cout<<"sqrt("<<deta/(sq*sq)<<")/"<<getop(a)*2*a/gd;
						else if(2*a==gd)cout<<sq/gd<<"*sqrt("<<deta/(sq*sq)<<")";
						else cout<<sq/gd<<"*sqrt("<<deta/(sq*sq)<<")/"<<getop(a)*2*a/gd;
						cout<<endl;
					}
				}
			}
		}
	}
	return 0;
}

2025/1/18 20:18
加载中...