萌新求助高斯消元90分WA#3

P2455 [SDOI2006] 线性方程组

qip101 @ 2022-05-22 17:18:10

#include <bits/stdc++.h>
#define eps 1e-6
using namespace std;
double a[205][205];
int n;
int main()
{
    ios::sync_with_stdio(false);
    cin >> n;
    for(register int i=1;i<=n;i++)
        for(register int j=1;j<=n+1;j++)
            cin >> a[i][j];
    for(register int i=1;i<=n;i++)
    {
        register int max=i;
        for(register int j=i+1;j<=n;j++)
            if(fabs(a[j][i])>fabs(a[max][i]))
                max=j;
        for(register int j=1;j<=n+1;j++)
            swap(a[i][j],a[max][j]);
        if(a[i][i]==0)
            continue;
        for(register int j=1;j<=n;j++)
        {
            if(j!=i)
            {
                for(register int k=i+1;k<=n+1;k++)
                    a[j][k]-=a[j][i]/a[i][i]*a[i][k];
            }   
        }
    }
    for(register int i=1;i<=n;i++)
        if(a[i][i]==0 && a[i][n+1]!=0)
        {
            cout << "-1" << endl;
            return 0;
        }
    for(register int i=1;i<=n;i++)
        if(a[i][i]==0 && a[i][n+1]==0)
        {
            cout << "0" << endl;
            return 0;
        }
    for(register int i=1;i<=n;i++)
        cout << fixed << setprecision(2) << "x" << i << "=" << a[i][n+1]/a[i][i]+eps << endl;
    return 0;
}

by Father_888 @ 2022-05-24 21:56:26

hello


by Father_888 @ 2022-05-24 21:56:43

qp


by Father_888 @ 2022-05-24 21:59:01

关注你一下吧(~ ̄▽ ̄)~


|