介绍:
先输入n, 然后输入n * n 矩阵,最后输出矩阵的值。
#include <bits/stdc++.h>
using namespace std;
float result;
int A[1010][1010];
float AA[1010][1010];
int n;
void Swap(float *a, float *b)
{
for(int i = 1; i <= n; ++i)
{
float temp = a[i];
a[i] = b[i];
b[i] = temp;
}
}
void GetValue()
{
float mul;
int iter = 0;
result = 1.0;
for(int i = 1; i <= n; ++i)
{
if(AA[i][i] == 0)
{
for(int k = i + 1; k <= n; ++k)
{
if(AA[k][i] != 0)
{
Swap(AA[i], AA[k]);
iter++;
break;
}
}
}
for(int k = i + 1; k <= n; ++k)
{
if(AA[k][i] != 0)
{
mul = -1 * (AA[k][i] / AA[i][i]);
AA[k][i] = 0;
for(int p = i + 1; p <= n; ++p)
{
AA[k][p] += mul * AA[i][p];
}
}
}
}
for(int i = 1; i <= n; ++i)
result *= AA[i][i];
if(iter % 2 == 1)
result = -result;
}
void Input()
{
scanf("%d", &n);
for(int i = 1; i <= n; ++i)
for(int j = 1; j <= n; ++j)
{
scanf("%d", &A[i][j]);
AA[i][j] = A[i][j];
}
}
void Putout()
{
for(int i = 1; i <= n; ++i)
{
for(int j = 1; j <= n; ++j)
cout << AA[i][j] << " ";
cout << endl;
}
}
int main()
{
Input();
GetValue();
Putout();
cout << result << endl;
}