题目链接:https://www.luogu.org/problemnew/show/P2455

无解:最后一列对应元素不为0,前面全是0.

无穷解:一行全是0.

嗯...在消元过程中不要直接拿矩阵元素自己消,会把自己消成0.

 #include <algorithm>
#include <cstdio>
#include <cmath>
#include <iostream>
using namespace std;
const int maxn = ;
const double eps = 1e-;
double A[maxn][maxn], ans[maxn];
int n;
int main()
{
scanf("%d",&n);
for(int i = ; i <= n; i++)
for(int j = ; j <= n+; j++)
scanf("%lf", &A[i][j]);
for(int i = ; i <= n; i++)
{
int p = i;
for(int j = i + ; j <= n; j++)
if(fabs(A[j][i]) > fabs(A[p][i])) p = j;
for(int j = ; j <= n + ; j++) swap(A[p][j],A[i][j]); if(fabs(A[i][i]) < eps) continue;
double div = A[i][i];
for(int j = ; j <= n + ; j++) A[i][j]/=div;
for(int j = ; j <= n; j++)
{
if(i != j)
{
double div = A[j][i];
for(int k = ; k <= n + ; k++) A[j][k] -= A[i][k]*div;
}
}
}
int NoSolution = , ManySolution = ;
for(int i = ; i <= n; i++)
{
int Nonum = , Manynum = ;
for(int j = ; j <= n + && fabs(A[i][j]) < eps; j++)
Nonum++,Manynum++;
if(Manynum > n) ManySolution = ;
if(Nonum == n) NoSolution = ;
}
if(NoSolution) {printf("-1");return ;}
if(ManySolution) {printf("");return ;}
for(int i = n; i >= ; i--)
{
ans[i] = A[i][n+];
for(int j = i - ; j >= ; j--)
{
A[j][n+] -= ans[i] * A[j][i];
A[j][i] = ;
}
}
for(int i = ; i <= n; i++)
printf("x%d=%.2lf\n",i,ans[i] + eps);
return ;
}
05-16 05:52