BorisDimitri @ 2022-06-15 16:28:45
高斯约旦消元
试过多个 #7
#8
#include <bits/stdc++.h>
using namespace std;
const int N = 55;
const double eps = 1e-8;
double a[N][N];
int n;
int main()
{
cin>>n;
for(int i=1;i<=n;i++)
for(int j=1;j<=n+1;j++)
cin>>a[i][j];
int line = 1;
for(int i=1;i<=n;i++)
{
int t = line;
for(int j=line+1;j<=n;j++)
if(fabs(a[t][i]) < fabs(a[j][i]))
t = j;
if(a[t][i] == 0) continue;
for(int j=1;j<=n+1;j++) swap(a[line][j], a[t][j]);
for(int j=1;j<=n;j++)
if(line != j)
{
double temp = a[j][i] / a[line][i];
for(int k=i+1;k<=n+1;k++)
a[j][k] -= a[line][k] * temp;
}
line ++;
}
if(line <= n)
{
while(line <= n)
if(a[line++][n] != 0)
{
cout<<-1;
return 0;
}
cout<<0;
return 0;
}
for(int i=1;i<=n;i++)
printf("x%d=%.2lf\n",i,a[i][n+1]/a[i][i]+eps);
return 0;
}