80pts求调,悬两关

P2455 [SDOI2006] 线性方程组

我梦见一片焦土 @ 2024-01-01 15:50:42

#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N=1e3+10;
const int INF=0x3f3f3f3f;
double a[N][N],b[N];
int n;
signed main(){
    cin>>n;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            cin>>a[i][j];
        }
        cin>>b[i];
    }
    for(int i=1;i<=n;i++){
        int k=i;
        for(int j=i+1;j<=n;j++){
            if(fabs(a[j][i])>fabs(a[k][i])){
                k=j;
            }
        }
        if(fabs(a[k][i])<1e-8){
            cout<<"-1"<<endl;
            return 0;
        }
        if(k!=i){
            for(int j=i;j<=n;j++){
                swap(a[k][j],a[i][j]);
            }
            swap(b[k],b[i]);
        }
        for(int j=1;j<=n;j++){
            if(i!=j){
                double w=a[j][i]/a[i][i];
                for(k=i;k<=n;k++){
                    a[j][k]-=a[i][k]*w;
                }
                b[j]-=b[i]*w;
            }
        }
    }
    for(int i=1;i<=n;i++){
        cout<<"x"<<i<<"=";
        printf("%.2lf\n",b[i]/a[i][i]);
    }
    return 0;
}

by GUO120822 @ 2024-02-01 19:24:48

你的 "0"呢


|