蒟蒻刚学高斯消元,80分被卡求助

P2455 [SDOI2006] 线性方程组

wubaiting2020 @ 2019-07-18 16:20:08

#include<algorithm>
#include<iostream>
#include<iomanip>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<queue>
#define LL long long
using namespace std;
const int INF=0x3f3f3f3f,MAXX=100005;
const double eps=0.0000001;
int n;
double a[105][105];
double abss(double x){if(x<0)return -x;return x;}
int main()
{
    scanf("%d",&n);
    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++)//跑到了第i个未知数 
    {
        int no=i;
        for(int j=i+1;j<=n;j++)if(abss(a[j][i])>abss(a[no][i]))no=j;
        for(int j=1;j<=n+1;j++)swap(a[i][j],a[no][j]);//将当前系数最大的换到第i行
        if(abss(a[i][i])>eps)
        {
            double x=a[i][i];
            for(int j=i;j<=n+1;j++)a[i][j]/=x;
            for(int j=1;j<=n;j++)
            for(int k=i+1;k<=n+1;k++)if(i!=j)a[j][k]-=a[j][i]*a[i][k];
        }
    }
    int bj1=0,bj2=0;
    for(int i=1;i<=n;i++)
    {
        int j=1;
        while(abss(a[i][j])<eps&&j<=n+1)j++;
        if(j>n+1)bj1=1;
        if(j==n+1)bj2=1; 
    }
    if(bj2){printf("-1");return 0;}
    if(bj1){printf("0");return 0;}
    for(int i=1;i<=n;i++)if(a[i][n+1]!=0)printf("x%d=%.2lf\n",i,a[i][n+1]);else printf("x%d=0\n",i);
    return 0;
}

by wubaiting2020 @ 2019-07-19 11:14:50

@谁是鸽王 我错的点都是0,-1但是我不知道我判断哪里错了


by lyhhahaha @ 2019-07-30 12:29:49

楼主你好,请问你找到你的程序哪里有问题了吗,我写的跟你差不多,也一直过不了。。。


by IOI_AKer_yyh @ 2019-08-05 21:02:31

+1


by caidzh @ 2019-08-12 14:59:45

@x义x 800+题,红名,qndjr


上一页 |