题目传送门
CCF特有的第三题大模拟(
这道题其实很简单(指考场爆零),之所以放在T3是因为在代码实现上有些难度
Talk is cheap, show me the code.
#include<bits/stdc++.h>
#define gcd(a,b) std::__gcd(abs(a),abs(b))
int T,m,a,b,c,delta;
void solve(){
scanf("%d%d%d",&a,&b,&c);
delta=b*b-4*a*c;
if(a<0)a=-a,b=-b,c=-c;
a*=2;
if(delta<0){
printf("NO\n");
return;
}
if(int(sqrt(delta)+.5)*int(sqrt(delta)+.5)==delta){
int up=int(sqrt(delta)+.5)-b;
if(up%a)printf("%d/%d\n",up/gcd(up,a),a/gcd(up,a));
else printf("%d\n",up/a);
return;
}
if(b){
printf("%d",-b/gcd(a,b));
if(b%a)printf("/%d",a/gcd(a,b));
printf("+");
}
int q2=sqrt(delta)+1.5,r=delta;
while(--q2&&delta%(q2*q2));
if(q2)r=delta/q2/q2;
if(a%q2)printf("%d*sqrt(%d)",q2/gcd(a,q2),r);
else printf("sqrt(%d)",r);
if(q2%a)printf("/%d",a/gcd(a,q2));
puts("");
}
main(){
for(scanf("%d%d",&T,&m);T;T--)solve();
}
运行结果(O2下)