O(n³)
double a[N][N];//增广矩阵
int gauss()
{
int c, r;
for (c = 0, r = 0; c < n; c++)
{
int t = r;
for (int i = r; i < n; i++)//寻找首位绝对值最大的行
{
if (fabs(a[i][c]) > fabs(a[t][c]))
{
t = i;
}
}
if (fabs(a[t][c]) < 1e-6)
{
continue;
}
for (int i = c; i <= n; i++)//将首位绝对值最大的行换到最顶端
{
swap(a[t][i], a[r][i]);
}
for (int i = n; i >= c; i--)//将当前行的首位变成1
{
a[r][i] /= a[r][c];
}
for (int i = r + 1; i < n; i++)//用当前行将下面所有的列消成0
{
if (fabs(a[i][c]) > 1e-6)
{
for (int j = n; j >= c; j--)
{
a[i][j] -= a[r][j] * a[i][c];
}
}
}
r++;
}
if (r < n)//矩阵不满秩
{
for (int i = r; i < n; i++)
{
if (fabs(a[i][n]) > 1e-6)
{
return 2;//无解
}
}
return 1;//无穷多解
}
for (int i = n - 1; i >= 0; i--)
{
for (int j = i + 1; j < n; j++)
{
a[i][n] -= a[i][j] * a[j][n];
}
if(fabs(a[i][n])<1e-6)//防止精度问题输出-0.00
{
a[i][n]=0;
}
}
return 0;//有唯一解
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n + 1; j++)
{
scanf("%lf", &a[i][j]);
}
}
for (int i = 0; i < n; i++) //res
{
printf("%.2lf\n", a[i][n]);
}