这里有哪编译错误了吗???
查看原帖
这里有哪编译错误了吗???
1082298
Ghosty_Neutrino楼主2024/6/11 21:39
#include <bits/stdc++.h>
using namespace std;
int T,m,a,b,c,delta;
bool square(int n){
	int x=sqrt(n);
	return x*x==n;
}
int gcd(int x,int y){
	while(y!=0){
		int t=x%y;
		x=y;
		y=t;
	}
	return x;
}
void p1(int x,int y){ 
	
	if(x%y==0){
		cout<<x/y;
		return;
	}
	
	if(x*y<0){
		cout<<"-";
		x=abs(x),y=abs(y);
	}
	int t=gcd(x,y);
	cout<<x/t<<"/"<<y/t;
}

int fac(int n){
	for(int i=sqrt(n);i>=1;i--){
		if(n%(i*i)==0) return i;
	}
}

void p2(int x,int y,string z){
	
	if(x%y==0){
		if(x/y!=1) cout<<x/y<<"*";
		cout<<z;
		return;
	} 
	int t=gcd(x,y);
	if(x/t!=1){
		cout<<x/t<<"*";
	}
	cout<<z<<"/"<<y/t;
} 
int main(){
	cin>>T>>m;
	while(T--){
		scanf("%d%d%d",&a,&b,&c);
		delta=b*b-4*a*c;
		
		if(delta<0){
			cout<<"NO"<<endl;
			continue;
		}
	
		if(square(delta)){
			if(a<0) p1(-b-sqrt(delta),2*a);
			else p1(-b+sqrt(delta),2*a);
			cout<<endl;
			continue;
		}
		
		if(b!=0){
			p1(-b,2*a);
			cout<<"+";
		}
		a=abs(a);
		int t=fac(delta);
		string st="sqrt("+to_string(delta/(t*t))+")";
		p2(t,2*a,st);
		cout<<endl;
	}
	return 0;
}
2024/6/11 21:39
加载中...