80分代码,为什么无解被判断成了无穷多组解?

P2455 [SDOI2006] 线性方程组

TempTest @ 2018-11-05 11:15:52

RT,最后两组数据WA掉了
先判断-1在判断0,但是最后两组应当是-1
程序输出了0,我手写了几组数据又都能判断出来
求大佬帮我看看那儿错了

#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#define MAXN 1001
using namespace std;
double A[MAXN][MAXN];
int n,flag,flag1,flag2;
int Gauss()
{
    for(int i=0,r=0;i<n;r=++i)
    {
        for(int j=i+1;j<n;j++)
            if(fabs(A[j][i])>fabs(A[r][i]))r=j;
        if(r!=i)swap(A[r],A[i]);
        for(int j=n;j>=i;--j)
            for(int k=i+1;k<n;k++)
                A[k][j]-=A[k][i]/A[i][i]*A[i][j];
    }
    //消元过程 
    for(int k=0;k<n;k++)
    {
        flag1=flag2=0;
        for(int i=0;i<n;i++)
            if(fabs(A[k][i])>=1e-15)flag1=true;
        if(fabs(A[k][n]>=1e-15))flag2=true;
        if(flag1==0&&flag2!=0)return -1;
        if(flag1==0&&flag2==0)return 0;
    }
    //判断,一行全为零则无解,除了最后一个外全为零无解
    for(int i=n-1;i>=0;--i)
    {
        for(int j=i+1;j<=n;j++)
            A[i][n]-=A[i][j]*A[j][n];
        A[i][n]/=A[i][i];
    }
    //回带 
    return 1;
}
int main()
{
    cin>>n;
    for(int i=0;i<n;i++)
    for(int j=0;j<=n;j++)
        scanf("%lf",&A[i][j]);
    if((flag=Gauss())==1)
    for(int i=0;i<n;i++)
    {
        if(fabs(A[i][n])<1e-15)printf("x%d=%d\n",i+1,0);
        else printf("x%d=%.2lf\n",i+1,A[i][n]);
    }
    else cout<<flag;
}

by CherYou @ 2018-11-06 11:26:27

include<iostream>

include<cstdio>

include<cmath>

include<cstring>

include<algorithm>

using namespace std; const int N=55; int n; double map[N][N+1]; double eps=1e-7; double ans[N]; void gauss() { int k=1,c=1; for(;k<=n&&c<=n+1;k++,c++) { int r=k; for(int i=k+1;i<=n;i++) if(fabs(map[i][c])>fabs(map[k][c])) r=i; if(fabs(map[r][c])<eps) {k--;continue;} if(r!=k) for(int i=c;i<=n+1;i++) swap(map[k][i],map[r][i]); double temp=map[k][c]; for(int i=c;i<=n+1;i++) map[k][i]/=temp; for(int i=k+1;i<=n;i++) { temp=map[i][c]; for(int j=c;j<=n+1;j++) map[i][j]-=tempmap[k][j]; } } // if(k<=n) {printf("0\n");return ;} // for(int i=k;i<=n;i++) if(fabs(map[i][n+1])>eps) {printf("-1\n");return;} int flag1=0,flag2=0; for(int i=1;i<=n;i++) { int j=1; while(fabs(map[i][j])<eps&&j<=n+1)j++; if(j>n+1)flag1=1; else if(j==n+1)flag2=1; } if(flag2){ printf("-1\n"); return ; } if(flag1){ printf("0\n"); return ; } ans[n]=map[n][n+1]; for(int i=n-1;i>=1;i--) { ans[i]=map[i][n+1]; for(int j=i+1;j<=n;j++) ans[i]-=ans[j]map[i][j]; } for(int i=1;i<=n;i++) { printf("x%d=%.2lf\n",i,ans[i]); } } int main() { scanf("%d",&n); for(int i=1;i<=n;i++) for(int j=1;j<=n+1;j++) { scanf("%lf",&map[i][j]); } gauss(); }


by CherYou @ 2018-11-06 11:26:58

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=55;
int n;
double map[N][N+1];
double eps=1e-7;
double ans[N];
void gauss()
{
    int k=1,c=1;
    for(;k<=n&&c<=n+1;k++,c++)
    {
        int r=k;
        for(int i=k+1;i<=n;i++) if(fabs(map[i][c])>fabs(map[k][c])) r=i;
        if(fabs(map[r][c])<eps) {k--;continue;}
        if(r!=k) for(int i=c;i<=n+1;i++) swap(map[k][i],map[r][i]);
        double temp=map[k][c]; 
        for(int i=c;i<=n+1;i++) map[k][i]/=temp;
        for(int i=k+1;i<=n;i++)
        {
            temp=map[i][c];
            for(int j=c;j<=n+1;j++) map[i][j]-=temp*map[k][j];
        } 
    }
//  if(k<=n) {printf("0\n");return ;}
//  for(int i=k;i<=n;i++) if(fabs(map[i][n+1])>eps) {printf("-1\n");return;}
    int flag1=0,flag2=0;
    for(int i=1;i<=n;i++)
    {
        int j=1;
        while(fabs(map[i][j])<eps&&j<=n+1)j++;
        if(j>n+1)flag1=1;
        else if(j==n+1)flag2=1;
    }
    if(flag2){
        printf("-1\n");
        return ;
    }
    if(flag1){
        printf("0\n");
        return ;
    }
    ans[n]=map[n][n+1];
    for(int i=n-1;i>=1;i--)
    {
        ans[i]=map[i][n+1];
        for(int j=i+1;j<=n;j++)
        ans[i]-=ans[j]*map[i][j];
    }
    for(int i=1;i<=n;i++)
    {
        printf("x%d=%.2lf\n",i,ans[i]);
    }
}
int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    for(int j=1;j<=n+1;j++)
    {
        scanf("%lf",&map[i][j]);
    }
    gauss();
}

|