WA on #2 #4 #9 #10 60pts
#include<bits/stdc++.h>
using namespace std;
int aa,bb,cc,t,m,f_son,f_mom;
void f(int a,int b,int c)
{
int sum=b*b-4*a*c;
if(sum<0)
{
cout<<"NO"<<endl;
return;
}
int op=sqrt(sum);
if(op*op==sum)
{
if(a>0)
{
f_son=op-b,f_mom=2*a;
}
else
{
f_son=-op-b,f_mom=2*a;
}
int g=__gcd(f_son,f_mom);
int g_son=f_son/g,g_mom=f_mom/g;
if(g_mom==1)
{
cout<<g_son;
}
else
{
if(g_mom>0)
{
cout<<g_son<<"/"<<g_mom;
}
else
{
cout<<-g_son<<"/"<<-g_mom;
}
}
cout<<endl;
return;
}
int k=sum,s=1,a2=2*a;
if(a2<0)
{
k=-k;
}
for(int i=1;i<=sqrt(k);i++)
{
if(k%(i*i)==0)
{
k/=(i*i);
s*=i;
}
}
int q=2*a,p=-b;
if(q!=0&&p!=0)
{
if(q==1)
{
cout<<p;
}
else
{
if(p%q==0)
{
cout<<p/q;
}
else
{
if(q>0)
{
cout<<p<<"/"<<q;
}
else
{
cout<<-p<<"/"<<-q;
}
}
}
cout<<"+";
}
if(s%a2==0&&s/a2==1)
{
cout<<"sqrt("<<k<<")"<<endl;
return;
}
if((s/a2)*a2==s)
{
cout<<s/a2<<"*sqrt("<<k<<")"<<endl;
return;
}
if(a2%s==0)
{
cout<<"sqrt("<<k<<")/"<<a2/s<<endl;
return;
}
int wg=__gcd(a2,s);
cout<<s/wg<<"*sqrt("<<k<<")/"<<a2/wg<<endl;
return;
}
int main()
{
cin>>t>>m;
for(int i=1;i<=t;i++)
{
cin>>aa>>bb>>cc;
f(aa,bb,cc);
}
return 0;
}
Thanks reading and helping!