暗影之梦 @ 2022-09-27 21:08:49
#include<iostream>
#include<cstdio>
#include<cmath>
#define int long long
using namespace std;
int n;
double a[101][102];
const double eps=1e-7;
signed main()
{
scanf("%lld",&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++)
{
int maxn=i;
for(int j=i+1;j<=n;j++)
{
if(fabs(a[j][i])>fabs(a[maxn][i])) maxn=j;
}
for(int j=1;j<=n+1;j++) swap(a[i][j],a[maxn][j]);
if(fabs(a[i][i])<eps)
{
continue;
// if(fabs(a[i][n+1])<eps) printf("0");
// else printf("-1");
// return 0;
}
for(int j=1;j<=n;j++)
{
if(i!=j)
{
double K=a[j][i]/a[i][i];
for(int k=i+1;k<=n+1;k++) a[j][k]-=a[i][k]*K;
}
}
}
for(int i=1;i<=n;i++)
{
if(fabs(a[i][i])<eps)
{
if(fabs(a[i][n+1])>eps&&fabs(a[i][i])<eps) printf("-1");
else if(fabs(a[i][n+1])<eps&&fabs(a[i][i])<eps) printf("0");
return 0;
}
}
for(int i=1;i<=n;i++)
{
if(fabs(a[i][n+1]/a[i][i])<eps) printf("x%lld=0.00\n",i);
else printf("x%lld=%.2lf\n",i,a[i][n+1]/a[i][i]);
}
return 0;
}
by Missa @ 2022-09-28 20:20:20
@暗影之梦 现在是 #9 WA,无解判有解
我也没弄懂为什么有解会判成无解
by Missa @ 2022-09-28 20:49:17
@暗影之梦 好了,经过神秘操作后能过了