#include<cstdio>
#include<algorithm>
#include<cstdlib>
using namespace std;
#define MAXN 400
int a[MAXN+10][MAXN+10],x[MAXN+10],n,m,row;
template<class T>
void Read(T &x){
char c;
bool f=0;
while(c=getchar(),c!=EOF){
if(c=='-')
f=1;
if(c>='0'&&c<='9'){
x=c-'0';
while(c=getchar(),c>='0'&&c<='9')
x=x*10+c-'0';
ungetc(c,stdin);
if(f)
x=-x;
return;
}
}
}
void read(){
Read(n),Read(m);
int i,j;
for(i=1;i<=n;i++)
for(j=1;j<=m+1;j++)
Read(a[i][j]);
}
int gcd(int a,int b){
int t;
while(b){
t=a%b;
a=b;
b=t;
}
return a;
}
void gaussian_elimination(){
int col,i,mxr,j,lcm;
for(row=col=1;row<=n&&col<=m;row++,col++){
mxr=row;
for(i=row+1;i<=n;i++)
if(abs(a[i][col])>abs(a[mxr][col]))
mxr=i;
if(mxr!=row)
swap(a[mxr],a[row]);
if(!a[row][col]){
row--;
continue;
}
for(i=row+1;i<=n;i++)
if(a[i][col]){
int t1=lcm/a[i][col],t2=lcm/a[row][col];
lcm=a[row][col]/gcd(a[row][col],a[i][col])*a[i][col];
for(j=m+1;j;j--)
a[i][j]=a[i][j]*t1-a[row][j]*t2;
}
}
row--;
}
void find_ans(){
if(row==m){
int i,j;
for(i=n;i;i--){
for(j=i+1;j<=m;j++)
a[i][m+1]-=a[i][j]*x[j];
x[i]=a[i][m+1]/a[i][i];
printf("x%d=%d\n",i,x[i]);
}
}
}
int main()
{
read();
gaussian_elimination();
find_ans();
}
转载于:https://www.cnblogs.com/outerform/p/5921878.html