60分求调(代码有点凌乱)
查看原帖
60分求调(代码有点凌乱)
723046
huziting楼主2024/11/21 20:56
#include<bits/stdc++.h>
using namespace std;
int a,b,c,t;
int gcd(int x,int y)
{
	if(y==0) return x;
	return gcd(y,x%y);
}
void solve1()
{
	int c=0,d=t;
	for(int i=sqrt(t);i>0;i--)
	{
		if(i*i==t)
		{
			c=i;
			d=0;
			break;
		}
		if(t%(i*i)==0)
		{
			c=i;
			d=t/(i*i);
			break;
		}
	}
	int x=abs(gcd(c,2*a)),y=abs(gcd(2*a,-b));
	if(c/x==1)
	{
		if(-b==0)
		{
			cout<<"sqrt("<<d<<")"<<endl;
		}
		else
		{
			if(2*a/y==1)
			{
				cout<<-b/y;
			}
			else
			{
				if(-b%(2*a)==0)
					cout<<-b/(a*2);
				else if(2*a/y==1)
					cout<<-b/y;
				else 
					cout<<-b/y<<"/"<<2*a/y;
			}
			if(2*a/x==1)
			{
				cout<<"+sqrt("<<d<<")"<<endl;
			}
			else
			{
				cout<<"+sqrt("<<d<<")/"<<2*a/x<<endl;
			}
		}
		
	}
	else
	{
		if(-b==0)
		{
			if(2*a/x==1)
			{
				cout<<c/x<<"*sqrt("<<d<<")"<<endl;
			}
			else if(c%(2*a)==0)
			{
				cout<<c/(2*a)<<"*sqrt("<<d<<")"<<endl;
			}
			else
				cout<<"sqrt("<<d<<")/"<<2*a/x<<endl;
		}
		else
		{
			if(2*a/y==1)
			{
				cout<<-b/y;
			}
			else
			{
				if(-b%(a*2)==0)
					cout<<-b/(2*a);
				else if(2*a/y==1)
					cout<<-b/y;
				else
					cout<<-b/y<<"/"<<2*a/y;
			}
			if(2*a/x==1)
			{
				cout<<"+"<<c/x<<"*sqrt("<<d<<")"<<endl;
			}
			else
			{
				cout<<"+"<<c/x<<"*sqrt("<<d<<")/"<<2*a/x<<endl;
			}
		}
	}
}
void solve()
{
	if(t<0)
	{
		cout<<"NO"<<endl;
	}
	else if(t==0)
	{
		int x=abs(gcd(b,2*a));
		if(2*a/x!=1)
		{
			if(-b%(2*a)==0)
				cout<<-b/(2*a)<<endl;
			else 
				cout<<-b/x<<"/"<<2*a/x<<endl;
		}
		else if(2*a/x==1)
			cout<<-b/x<<endl;
	}
	else if(t>0)
	{
		if((int)sqrt(t)*(int)sqrt(t)==t)
		{
			int x1=(int)sqrt(t)-b,x=abs(gcd(x1,2*a));
			if(x1!=0)
			{
				if(2*a/x!=1)
					cout<<x1/x<<"/"<<2*a/x<<endl;
				else
					cout<<x1/x<<endl;
			}
			else
			{
				cout<<0<<"\n";
			}
		}
		else
		{
			solve1();
		}
	}
}
int main()
{
	//freopen("P9750_2.in","r",stdin);
	//freopen("uqe2out.out","w",stdout);
	int T,M;
	cin>>T>>M;
	while(T--)
	{
		cin>>a>>b>>c;
		if(a<0)
		{
			a=-a;
			b=-b;
			c=-c;
		}
		t=b*b-4*a*c;
		solve();
	}
	return 0;
}
2024/11/21 20:56
加载中...