WA #4 ,求助 (高斯——约旦)

P2455 [SDOI2006] 线性方程组

叶枫 @ 2019-08-21 09:35:13

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<string>
#include<queue>
#define ll long long
#define maxn 110
#define inf 2147483647
#define mod 1000000007
using namespace std; 
inline int read(){
    int x=0,f=1; char ch=getchar();
    while(!isdigit(ch)) {if(ch=='-')f=-1;ch=getchar();}
    while(isdigit(ch)) {x=x*10+ch-48;ch=getchar();}
    return x*f;
}
double a[maxn][maxn];
signed main(){
    int n=read();
    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++){
        int maxx=i;
        for(int j=i+1;j<=n;j++) 
            if(fabs(a[j][i])>fabs(a[maxx][i])) maxx=j;
        for(int j=1;j<=n+1;j++) swap(a[i][j],a[maxx][j]);
        if(!a[i][i]) continue;
        for(int j=1;j<=n;j++)
        if(j!=i){
            double temp=a[j][i]/a[i][i];
            for(int k=i+1;k<=n+1;k++)
            a[j][k]-=a[i][k]*temp;
        }
    }
    for(int i=1;i<=n;i++) {
        if(!a[i][i]&& a[i][n+1]) {printf("-1");return 0;}
        if(!a[i][i]&&!a[i][n+1]) {printf("0"); return 0;}
    }
    for(int i=1;i<=n;i++)
    printf("x%d=%.2lf\n",i,!a[i][n+1]/a[i][i]?0:a[i][n+1]/a[i][i]);
    return 0;
}

by xyf007 @ 2019-08-21 09:54:26

直接写高斯就好了


|