//用求上三角的方法求行列式的值
#include <cstdlib>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
//int a[n][n]={{1, 2, 3 ,4},{2 ,3 ,4 ,1},{3 ,4 ,1 ,2},{4, 1, 2, 3}};
int n;
freopen("data.txt","r",stdin);//请在工程项目目录下新建data.txt,将行列式的阶数及行列式信息输入
/*for example
3
1 2 -3
-2 3 -1
1 -2 8
*/
freopen("res.txt","w",stdout);//结果输出
cin>>n;
double a[n][n];
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
cin>>a[i][j];
int sign=1;
double tmp[n];
int p;
for(int col=0;col<n;col++)
{
int row;
p=-1;
for(row=col;row<n;row++)
{
if(a[row][col]!=0)
{
p=row; break; //确保当前的值不为零,否则下面将出现错误,算法要求出右下的不为零的点
}
}
if(-1==p) {cout<<"0"<<endl;return 0;}
if(p!=col) //交换
{
for(int j=0;j<n;j++)
{
tmp[j]=a[col][j];
a[col][j]=a[p][j];
a[p][j]=tmp[j];
}
sign=-sign; // 交换变号
}
for(int row=col+1;row<n;row++)
{
double factor=a[row][col]/a[col][col];//确保a[col][col]不为零
a[row][col]=0;
for(int _col=col+1;_col<n;_col++)
{
a[row][_col]-=a[col][_col]*factor;
}
}
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
cout<<a[i][j]<<" ";
cout<<endl;
}
cout<<endl;
}
double res=1.0;
for(int i=0;i<n;i++)
{
res*=a[i][i];
}
cout<<endl;
res*=sign;
cout<<res<<endl;
}