CODE
#include<bits/stdc++.h>
using namespace std;
const double eps=1e-10;
double uu,a[52][52],b[52];
int n,l[52];bool pd;
inline void zzd(int &maxx,int i,int cnt){
for(int j=cnt+1;j<=n;++j){//找系数最大值
if(fabs(a[j][i])>fabs(a[maxx][i]))
maxx=j;
}
return ;
}
signed main(void){
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);
}int cnt=1;
for(int i=1;i<=n;++i){//枚举列
int maxx=cnt;
zzd(maxx,i,cnt);
if(fabs(a[maxx][i])<eps) continue;
for(int j=1;j<=n;++j) swap(a[cnt][j],a[maxx][j]);//交换
swap(b[cnt],b[maxx]);
uu=a[cnt][i];
for (int j = i; j <= n; j++)
a[cnt][j]/=uu;
b[cnt]/=uu;
for(int j=1;j<=n;++j){
if(j!=cnt){
uu=a[j][i];
for(int k=i;k<=n;++k)
a[j][k]-=a[cnt][k]*uu;
b[j]-=b[cnt]*uu;
}
}
l[cnt]=i;++cnt;
}
for(int i=1;i<=n;++i){
if(fabs(a[i][l[i]])<eps){
if(fabs(b[i])>=eps) return puts("-1"),0;
pd=1;continue;
}
}
if(pd) return puts("0")&0;
double ans;
for(int i=1;i<=n;++i)
{
ans=b[i]/a[i][i];
if(ans==0) printf("x%d=0\n",i);
else printf("x%d=%.2lf\n",i,ans);
}
return 0;
}
标签:maxx,return,int,double,52,高斯消,pd,模板
From: https://www.cnblogs.com/GOD-HJ/p/17321232.html