参考程序代码:
#include<bits/stdc++.h>
using namespace std;
int t,m,a,b,c;
int aa,bb,gd1,gd2;
int gcd(int a,int b)
{
if(a %b ==0)return b;
return gcd(b,a %b);
}
int main()
{
scanf("%d%d",&t,&m);
while(t--)
{
scanf("%d%d%d",&a,&b,&c);
int det =b*b-4*a*c;
if(det <0)
{
printf("NO\n");
continue;
}
int ans1 =1;
for(int i=2;i*i <=det;i++)
{
while(det %(i*i)==0)
ans1 *=i,det /=i*i;
}
aa =2 *a;
bb =-b;
if(aa <0)
aa =-aa,bb=-bb;
if(det ==1)det =0,bb +=ans1;
gd1 = gcd(abs(bb),aa);
gd2 = gcd(ans1,aa);
if(det ==0)
{
if(bb %aa ==0) printf("%d",bb /aa);
else printf("%d/%d",bb /gd1,aa /gd1);
}
else
{
if(bb !=0)
{
if(bb %aa ==0) printf("%d",bb /aa);
else printf("%d/%d",bb /gd1,aa /gd1);
printf("+");
}
if(ans1 /gd2 !=1) printf("%d*",ans1 /gd2);
printf("sqrt(%d)",det);
if(aa /gd2 !=1) printf("/%d",aa /gd2);
}
printf("\n");
}
return 0;
}
参考程序考试文件提交代码:
#include<bits/stdc++.h>
using namespace std;
int t,m,a,b,c;
int aa,bb,gd1,gd2;
int gcd(int a,int b)
{
if(a %b ==0)return b;
return gcd(b,a %b);
}
int main()
{
//打开输入文件,输出文件
freopen("uqe.in","r",stdin);
freopen("uqe.out","w",stdout);
scanf("%d%d",&t,&m);
while(t--)
{
scanf("%d%d%d",&a,&b,&c);
int det =b*b-4*a*c;
if(det <0)
{
printf("NO\n");
continue;
}
int ans1 =1;
for(int i=2;i*i <=det;i++)
{
while(det %(i*i)==0)
ans1 *=i,det /=i*i;
}
aa =2 *a;
bb =-b;
if(aa <0)
aa =-aa,bb=-bb;
if(det ==1) det =0,bb +=ans1;
gd1 = gcd(abs(bb),aa);
gd2 = gcd(ans1,aa);
if(det ==0)
{
if(bb %aa ==0) printf("%d",bb /aa);
else printf("%d/%d",bb /gd1,aa /gd1);
}
else
{
if(bb !=0)
{
if(bb %aa ==0) printf("%d",bb /aa);
else printf("%d/%d",bb /gd1,aa /gd1);
printf("+");
}
if(ans1 /gd2 !=1) printf("%d*",ans1 /gd2);
printf("sqrt(%d)",det);
if(aa /gd2 !=1) printf("/%d",aa /gd2);
}
printf("\n");
}
//关闭输入文件输出文件
fclose(stdin);
fclose(stdout);
return 0;
}
标签:2023CSP,return,试题,int,scanf,d%,det,一元二次方程,gcd
From: https://blog.csdn.net/weixin_60445850/article/details/142458866