社区讨论
30分,求助
P9750[CSP-J 2023] 一元二次方程参与者 1已保存回复 1
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 1 条
- 当前快照
- 1 份
- 快照标识符
- @m1yqx29e
- 此快照首次捕获于
- 2024/10/07 16:24 去年
- 此快照最后确认于
- 2025/11/04 17:42 4 个月前
CPP
#include <bits/stdc++.h>
using namespace std;
double a,b,c;
int n,m;
double drt,xx1,xx2,xm,xa;
void fs(int up,int down);
void gh(int b,int a2,int d,int f);
int main()
{
cin>>n>>m;
for(int i=1; i<=n; i++)
{
cin>>a>>b>>c;
drt=b*b*1.0-4.0*a*c;
if(drt>=0)
{
xx1=(b-b*2.0)+sqrt(drt);
xx2=(b-b*2.0)-sqrt(drt);
xm=max(xx1/(a*2.0),xx2/(a*2.0));
if(xm==int(xm)) cout<<int(xm)<<endl;
else if(xx1 == int(xx1) || xx2==int(xx2))
{
if(xm==xx1/(a*2.0)) fs(xx1,2*a);
else fs(xx2,2*a);
cout<<endl;
}
else
{
if(xx1*1.0/(a*2.0)>=xx2*1.0/(a*2.0)) gh(b-b*2,a*2,drt,1);
else gh(b-b*2,a*2,drt,2);
cout<<endl;
}
}
else cout<<"NO";
}
return 0;
}
void fs(int up,int down)
{
if(up==0) cout<<'0';
else
{
int gcd=abs(__gcd(up,down));
if(down==1) cout<<up;
else if(down==-1)
{
if(up>0) cout<<'-'<<up;
else cout<<abs(up);
}
else if(up%down==0) cout<<up/down;
else if(down<0 && up>0 || down>0 && up<0)
{
cout<<'-';
cout<<abs(up)/gcd<<'/'<<abs(down)/gcd;
}
else cout<<abs(up)/gcd<<'/'<<abs(down)/gcd;
}
return;
}
void gh(int b,int a2,int d,int f)
{
if(b!=0) fs(b,a2);
int xx1=1,xx2=0;
bool flag=0;
for(int i=2; i<=d; i++)
{
if(d%i==0)
{
if(sqrt(d*1.0/i) == int(sqrt(d*1.0/i)) )
{
xx1=sqrt(d/i);
xx2=i;
flag=1;
break;
}
}
}
if(xx1==0 || xx2==0)
{
xx2=d;
xx1=1;
}
if(f==1)
{
if(a2<0) cout<<'-';
else if(b!=0) cout<<'+';
}
else if(f==2)
{
if(a2<0 && b!=0) cout<<'+';
else cout<<'-';
}
if(xx1%a2==0)
{
if(abs(xx1/a2)!=1) cout<<abs(xx1)/abs(a2)<<'*';
printf("sqrt(%d)",xx2);
}
else if( a2%xx1==0) printf("sqrt(%d)/%d",xx2,abs(a2)/abs(xx1));
else
{
cout<<abs(xx1)<<'*';
printf("sqrt(%d)/%d",xx2,abs(a2));
}
return;
}
回复
共 1 条回复,欢迎继续交流。
正在加载回复...