求代码优化(玄关)
查看原帖
求代码优化(玄关)
1388903
sunruiqi楼主2025/2/5 20:53
#include<bits/stdc++.h>
using namespace std;
int main(){
    double x1,x2,y1,y2;
    cin>>x1>>y1>>x2>>y2;
    double k=(y1-y2)/(x1-x2);
    double b=(x1*y2-x2*y1)/(x1-x2);
    if(k==(int)k){
        if(k>0)
            cout<<"y="<<k<<"x";
        else cout<<"y=-"<<abs(k)<<"x";
    }
    else {
        int m=y1-y2,n=x1-x2;
        int a=gcd(m,n);
        if(n*m>0)
            cout<<"y="<<abs(m/gcd(m,n))<<"/"<<abs(n/gcd(m,n))<<"*x";
        else cout<<"y=-"<<abs(m/gcd(m,n))<<"/"<<abs(n/gcd(m,n))<<"*x";
    }
    if(b==0)
        return 0;
    else if(b==(int)b){
        if(b>0)
            cout<<"+"<<b;
        else cout<<"-"<<abs(b);
    }
    else{
        int m=x1*y2-x2*y1,n=x1-x2;
        int a=gcd(m,n);
         if(n*m>0)
            cout<<"+"<<abs(m/gcd(m,n))<<"/"<<abs(n/gcd(m,n));
        else cout<<"-"<<abs(m/gcd(m,n))<<"/"<<abs(n/gcd(m,n));
    }
    return 0;
}

大佬们应该没写这么长吧,怎样精简代码,求优化。

2025/2/5 20:53
加载中...