//lg 2455
#include<bits/stdc++.h>
using namespace std;
const double eps = 0.000001;
const int N = 105;
double a[N][N];
int n;
int nowline=1;//存储当前行
void GaussJordan()
{
for(int i=1;i<=n;i++) //枚举列 若方程有唯一解 则与枚举行列等效
{
int mxline=nowline;
for(int k=i;k<=n;k++) if(fabs(a[k][i])>fabs(a[mxline][i])) mxline=k;
if(fabs(a[mxline][i])<eps) continue;
if(mxline!=nowline) swap(a[mxline],a[nowline]);
for(int k=1;k<=n;k++)
{
if(k==nowline) continue;
double t=a[k][i]/a[nowline][i];
for(int j=i;j<=n+1;j++) a[k][j]-=t*a[nowline][j];
}
nowline++;
}
if(nowline==n+1)
{
for(int i=1;i<=n;i++) printf("x%d=%.2lf\n",i,a[i][n+1]/a[i][i]);
return ;
}
while(nowline<n+1)
{
if(fabs(a[nowline][n+1])>eps)
{
cout<<-1;
return ;
}
nowline++;
}
cout<<0;
return ;
}
int main()
{
cin>>n;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n+1;j++)
{
cin>>a[i][j];
}
}
GaussJordan();
return 0;
}
标签:mxline,const,int,double,GaussJordan,可判,板子,元法
From: https://www.cnblogs.com/yeyou26/p/17990718