cmrhhh @ 2024-01-03 15:43:32
大概是0的情况也输出-1了,求hack
#include<bits/stdc++.h>
using namespace std;
const double eps=1e-7;
const int maxn=1e3+10;
double a[maxn][maxn],ans[maxn],bj[maxn],lt,op=0;
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];
if(n==1&&a[1][1]==0&&a[1][2]){cout<<-1;return 0;}
for(int i=1;i<=n;i++){
int r=i;
for(int j=i+1;j<=n;j++)if(fabs(a[j][i])>fabs(a[r][i]))r=j;
if(fabs(a[r][i])<eps){op=999;continue;}
double d=a[r][i];
if(i!=r)swap(a[i],a[r]);
for(int j=i;j<=n+1;j++){a[i][j]/=d;}
for(int j=i+1;j<=n;j++){
d=a[j][i];
for(int k=i;k<=n+1;k++)a[j][k]-=a[i][k]*d;
}
}
// for(int i=1;i<=n;i++){
// for(int j=1;j<=n;j++)cout<<a[i][j]<<" ";
// cout<<"\n";
// }
ans[n]=a[n][n+1];
int sum=n-1;
while(sum>=1){
for(int i=n-1;i>=1;i--){
if(a[i][sum]){bj[i]=1;
ans[sum]=a[i][n+1];
for(int j=sum+1;j<=n;j++)ans[sum]-=a[i][j]*ans[j];
break;
}
}
sum--;
}
for(int i=1;i<=n-1;i++)if(bj[i]==0){cout<<-1;return 0;}
if(op==999){cout<<0;return 0;}
for(int i=1;i<=n;i++)printf("x%d=%.2lf\n",i,ans[i]);
return 0;
}
//4
//0 0 2 1 2
//0 0 1 1 1
//0 0 0 1 1
//0 0 0 0 0