80pts,WA on#3#7#11

P2455 [SDOI2006] 线性方程组

Into_the_Abyss @ 2024-08-04 16:43:38

初学高斯消元

求调

#include<bits/stdc++.h>
#define ll long long
#define esp 1e-6
using namespace std;
const int N=105;
double a[N][N];

int main(){
    int n;
    cin>>n;
    for(int i=1;i<=n;i++)for(int j=1;j<=n+1;j++)cin>>a[i][j];
    for(int i=1;i<=n;i++){
        int p=i;
        for(int j=i+1;j<=n;j++){
            if(fabs(a[j][i])>fabs(a[p][i]))p=j;
        }
        for(int j=1;j<=n+1;j++)swap(a[i][j],a[p][j]);
        if(fabs(a[i][i])<esp){
            int key;
            if(abs(a[i][n+1])>esp) key=-1;
            else key=0;
            cout<<key;
            return 0;
        }
        for(int j=n+1;j>=1;j--)a[i][j]/=a[i][i];
        for(int j=1;j<=n;j++){
            if(j!=i){
                double q=a[j][i]/a[i][i];
                for(int k=1;k<=n+1;k++){
                    a[j][k]-=a[i][k]*q;
                }
            }
        }
    }

    for(int i=1;i<=n;i++)printf("x%d=%.2lf\n",i,a[i][n+1]);
    return 0;
}

by Into_the_Abyss @ 2024-08-04 16:44:27

可以给组hack吗


by Into_the_Abyss @ 2024-08-04 16:45:55

判断无解和无数解有问题


|