我就是天帝 @ 2021-02-07 19:43:30
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
const double eps=1e-7;
const int N=110;
int T,n;
double a[N][N],ans[N],b[N];
double F(double A)
{
if(A<0) return -A;
return A;
}
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
scanf("%lf",&a[i][j]);
scanf("%lf",&b[i]);
}
for(int i=1;i<=n;i++)
{
int Max=i;
for(int j=i+1;j<=n;j++)
if(F(a[j][i])>F(a[Max][i])) Max=j;
for(int l=i;l<=n;l++) swap(a[i][l],a[Max][l]);
swap(b[i],b[Max]);
if(F(a[i][i])<eps) continue;
for(int j=i+1;j<=n;j++)
{
double k=a[j][i]/a[i][i];
for(int l=i;l<=n;l++)
a[j][l]-=k*a[i][l];
b[j]-=k*b[i];
}
}
for(int i=n;i>=1;i--)
{
for(int j=n;j>i;j--)
b[i]-=ans[j]*a[i][j];
ans[i]=b[i]/a[i][i];
}
for(int i=1;i<=n;i++)
{
if(F(a[i][i])<eps)
{
if(!b[i]) {printf("0");return 0;}
else T=1;
}
}
if(T) {printf("-1");return 0;}
for(int i=1;i<=n;i++)
{
if(ans[i]==-0) ans[i]=0;
printf("x%d=%.2lf\n",i,ans[i]);
}
return 0;
}
by loading777jzr @ 2022-01-26 15:21:53
我也卡在#9了,那现在知道什么原因了吗