CSPJ-3:uqe

原题:洛谷P9750

代码实现:

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
void gcd(int d,int e){
    int f,i=2;
    f=min(abs(d),e);
    while(i<=f){
          if(d%i==0&&e%i==0){
            d=d/i;
            e=e/i;
          }
          else{
            i++;
          }
    }
    if(e==1){
        cout<<d;
        }
    else{
        cout<<d<<'/'<<e;
    }
}

bool gcd1(int d){
    int n=0;
    for(int i=2;i*i<d;i++){
            if((d%(i*i))==0)n=i;
    }
    if(n!=0)return true;
    else return false;
}

bool gcd2(int d){
    for(int i=1;i<d;i++){
        if(d==i*i)return true;
    }
    return false;
}
int main(){
    int n,m;
    int f,g,h;
    cin>>n>>m;
    vector<int> a(n+1,0);
    vector<int> b(n+1,0);
    vector<int> c(n+1,0);

    for(int i=1;i<=n;i++){
        cin>>f>>g>>h;
        if(f>0){
            a[i]=f;
            b[i]=g;
            c[i]=h;
        }
        else{
          a[i]=-f;
          b[i]=-g;
          c[i]=-h;
        }
    }
    int delt;
    for(int i=1;i<=n;i++){
        delt=b[i]*b[i]-4*a[i]*c[i];
        //cout<<delt<<'\n';
        if(delt==0&&b[i]==0){
            cout<<0<<'\n';
          continue;
        }
        if(delt<0)cout<<"NO";
        if(delt==0){
            if(b[i]==0)cout<<0;
             else gcd(-b[i],2*a[i]);
            }
        if(delt==1){
            if((-b[i]+1)==0)cout<<0;
            else gcd(-b[i]+1,2*a[i]);
            
        }
        if(delt>0&&delt!=1){
            if((b[i]!=0)&&(!gcd2(delt))){
                gcd(-b[i],2*a[i]);
               cout<<'+';
                
            }
            
            if(gcd2(delt)){
               // cout<<"hello"<<'\n';
               if((-b[i]+sqrt(delt))==0)cout<<0;
                else gcd(-b[i]+sqrt(delt),2*a[i]);
            }
            else if(gcd1(delt)){
                 int n=0;
                 for(int i=2;i*i<delt;i++){
                    if(delt%(i*i)==0)n=i;
                 }
                 
                 int m=n;
                 int e=2*a[i];
                 int f=min(m,e);
                 int i=2;
                while(i<=f){
                    if(m%i==0&&e%i==0){
                        m=m/i;
                        e=e/i;
                    }
                    else{
                    i++;
                    }
                    }
                if(e==1){
                     if(m!=1)cout<<m<<'*'<<"sqrt("<<delt/(n*n)<<")";
                       else cout<<"sqrt("<<delt/(n*n)<<")";
                     }
                                
                else {
                    if(m!=1)cout<<m<<'*'<<"sqrt("<<delt/(n*n)<<")/"<<e;
                     else cout<<"sqrt("<<delt/(n*n)<<")/"<<e;
                }
          }
        else{             
            cout<<"sqrt("<<(b[i]*b[i]-4*a[i]*c[i])<<")/"<<2*a[i];
            }
        }
    cout<<'\n';
    
    }
}
Scroll to Top