60分,WA,solve函数有问题,求助
查看原帖
60分,WA,solve函数有问题,求助
1165114
zs_onepice楼主2024/10/19 09:36
#include <bits/stdc++.h>
#define int long long
using namespace std;
void ZorF(int a,int b){
    if(a==0){
        cout<<0;
        return;
    }
    if(a%b==0){
        cout<<a/b;
        return;
    }
    if(a*b<0){
        cout<<"-";
        if(a<0){
            a=-a;
        }
        if(b<0){
            b=-b;
        }
    }
    int g=__gcd(a,b);
    cout<<a/g<<"/"<<b/g;
}
void solve(int a,int b){
	bool flag=false;
    if(b<0){
        b=-b;
        flag=true;
    }
    int t=1;
    for(int i=sqrt(a);i>=2;i--){
        if(a%(i*i)==0){
            t=i;
			a/=(i*i);
            break;
        }
    }
    int g=__gcd(t,b);
    t/=g,b/=g;
    if(t!=1){
        cout<<t<<"*";
    }
    cout<<"sqrt("<<a<<")";
    if(b!=1){
        cout<<"/";
		if(flag){
			cout<<-b;	
		}
		else{
			cout<<b;
		}
    }
}
signed main(){
    int t,m;
    cin>>t>>m;
    for(int i=1;i<=t;i++){
        int a,b,c;
        cin>>a>>b>>c;
        int num=b*b-4*a*c;
        if(num<0){//无解
            cout<<"NO";
        }
        else{//有解
            int d=sqrt(num);
            if(d*d==num){//有理数
                double a1=1.0*(-b+d)/(2*a);
                double a2=1.0*(-b-d)/(2*a);
                if(a1>a2){
                    ZorF((-b+d),(2*a));
                }
                else{
                    ZorF((-b-d),(2*a));
                }
            }
            else{//无理数
                if(b!=0){
                    ZorF(-b,2*a);
                    cout<<"+";
                }
               	solve(d,2*a);
            }
        }
        cout<<endl;
    }
    return 0;
}

2024/10/19 09:36
加载中...