Problem Description
answer
Gaussian elimination, is used to solve \ (n-\) Element Method once Equations, time complexity \ (O (n ^ 3) \)
So forming the matrix equations
The goal is to the left of the matrix \ (n \ times n \) eliminate the identity matrix
\(\mathrm{Code}\)
#include<bits/stdc++.h>
using namespace std;
void read(int &x){
x=0;char ch=1;int fh;
while(ch!='-'&&(ch<'0'||ch>'9')) ch=getchar();
if(ch=='-') fh=-1,ch=getchar();
else fh=1;
while(ch>='0'&&ch<='9'){
x=(x<<1)+(x<<3)+ch-'0';
ch=getchar();
}
x*=fh;
}
#define maxn 107
int n;
double a[maxn][maxn];
int pla;
int main(){
ios::sync_with_stdio(0);
cin>>n;
for(register int i=1;i<=n;i++){
for(register int j=1;j<=n+1;j++) cin>>a[i][j];
}
for(register int i=1;i<=n;i++){
pla=i;
while(pla<=n&&a[pla][i]==0) pla++;
if(pla==n+1){//如果第i列没有非0的,显然无解
puts("No Solution");return 0;
}
for(register int j=1;j<=n+1;j++) swap(a[i][j],a[pla][j]);//交换到第i行
double tmp=a[i][i];
for(register int j=1;j<=n+1;j++) a[i][j]=a[i][j]/tmp;//消除第i行
for(register int j=1;j<=n;j++){
if(i==j) continue;
double rp=a[j][i];
for(register int k=1;k<=n+1;k++){
a[j][k]=a[j][k]-rp*a[i][k];//消除其他
}
}
}
for(register int i=1;i<=n;i++){
cout<<fixed<<setprecision(2)<<a[i][n+1]<<endl;
}
return 0;
}