蒟蒻刚学高斯消元,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 lzxy @ 2019-07-18 16:39:35

红名还蒟蒻


by wubaiting2020 @ 2019-07-18 17:00:42

@lzxy 我这么弱,我也不知道怎么红的QAQ


by x义x @ 2019-07-18 17:27:37

600+题,红名,qndjr


by 谁是鸽王 @ 2019-07-18 18:39:42

精度太低,inf太小 @wubaiting2020


by wubaiting2020 @ 2019-07-19 08:59:09

谢谢,但是改了还是80 @谁是鸽王


by 谁是鸽王 @ 2019-07-19 09:02:16

@wubaiting2020

还是精度低了 继续调小eps


by wubaiting2020 @ 2019-07-19 09:11:28

#include<algorithm>
#include<iostream>
#include<iomanip>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<queue>
#define LL long long
using namespace std;
const long double eps=0.00000000000001;
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 09:11:48

@谁是鸽王


by wubaiting2020 @ 2019-07-19 09:14:59

#include<algorithm>
#include<iostream>
#include<iomanip>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<queue>
#define LL long long
#define LD long double
using namespace std;
const int INF=0x3f3f3f3f,MAXX=100005;
const LD eps=0.00000000001;
int n;
LD a[105][105];
LD abss(LD 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)
        {
            LD 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;
}

我把全部换成long double了


by 谁是鸽王 @ 2019-07-19 10:32:28

@wubaiting2020

消元好像没错,是题目其他部分错了

不敢保证...


| 下一页