八十分 蒟蒻求调

P2455 [SDOI2006] 线性方程组

End1essSummer @ 2022-08-19 11:07:36

RT

Code:

#include <bits/stdc++.h>
#define int long long
using namespace std;
//System of linear equations线性方程组 ==Sole
//Linear correlation 线性相关  ==Lc
//#define double long double
const double eps=1e-8;
double Sole[110][110],x[114];
int n;
signed main(){
    cin>>n;
    for(int i=1;i<=n;i++)
        for(int j=1;j<=n+1;j++)
            cin>>Sole[i][j];
    for(int i=1;i<=n;i++){//Gauss消元
        int tmp=i;
        for(int j=i+1;j<=n;j++)
            if(Sole[j][i]>Sole[tmp][i])tmp=j;
        if(tmp!=i)
            //for(int k=1;k<=n+1;k++)
                swap(Sole[i],Sole[tmp]);
        for(int j=i+1;j<=n;j++){
            double nums=Sole[j][i]/Sole[i][i];
            for(int k=i;k<=n+1;k++){
                //if((Sole[j][k])==0) continue;
                Sole[j][k]-=Sole[i][k]*nums;
            }
        }
    }
    int tmp=0;
    for(int i=n;i>=1;i--){//带回求值
        double sum=0;
        for(int j=n;j>i;j--){
            sum+=x[j]*Sole[i][j];
        }if((Sole[i][n+1]-sum)==0&&Sole[i][i]==0){
            tmp++;
        }if((Sole[i][n+1]-sum)!=0&&Sole[i][i]==0){
            cout<<"-1\n";
            return 0;
        }
        x[i]=(Sole[i][n+1]-sum)/Sole[i][i];
    }
    /*if(tmp){
        cout<<"0\n";
        return 0;
    }*/
    for(int i=1;i<=n;i++){
        printf("x%lld=%.2lf\n",i,x[i]+eps);
    }
    return 0;
}

by End1essSummer @ 2022-08-19 11:08:38

死于#3 #7 这两个点应该输出0我输出了-1


|