求调 90pts

P2455 [SDOI2006] 线性方程组

I_like_play_eggy @ 2024-08-10 06:47:01

WA on #3,#11,#13

#include <bits/stdc++.h>
using namespace std;
const double eps=1e-7;
int n;
double a[56][56];
void print(){
    for (int i=1;i<=n;i++){
        for (int j=1;j<=n+1;j++){
            printf("%.2lf ",a[i][j]);
        }
        printf("\n");
    }
}
int main(){
    scanf("%d",&n);
    for (int i=1;i<=n;i++){
        for (int j=1;j<=n+1;j++){
            scanf("%lf",&a[i][j]);
        }
    }
    for (int i=1;i<=n;i++){
        for (int j=i;j<=n;j++){
            if (fabs(a[j][i])-fabs(a[i][i])>eps){
                swap(a[i],a[j]);
            }
        }
        if (fabs(a[i][i]-0.0)<eps) continue;
        for (int j=n+1;j>=i;j--){
            a[i][j]=a[i][j]*1.0/a[i][i];
//          cout<<a[i][j]<<" "<<a[i][i]<<endl;
        }
        for (int j=n;j>=1;j--){
            if (j==i) continue;
            for (int k=i+1;k<=n+1;k++){
//              if (k==i) continue;
                a[j][k]-=a[j][i]*a[i][k];
//              cout<<i<<j<<k<<a[2][2]<<endl;
            }
        }
        for (int j=1;j<=n;j++) if (j!=i) a[j][i]=0;
//      print();
    }
    for (int i=1;i<=n;i++){
        if (fabs(a[i][i]-0.0)<eps){
            if (fabs(a[i][n+1]-0.0)<eps) printf("0");
            else printf("-1");
            return 0;
        }
    }
    for (int i=1;i<=n;i++){
        printf("x%d=%.2lf\n",i,a[i][n+1]);
    }
    return 0;
}

|