Andy2035 @ 2023-08-04 21:50:43
输入
3
0.00 0.00 1.00 1.00
0.00 0.00 0.00 0.00
0.00 0.00 0.00 2.00
输出
-1
而这份AC代码输出0
#include<bits/stdc++.h>
using namespace std;
const int N = 55;
const double eps = 1e-8;
double a[N][N];
int n;
int gauss(){
int r,c;
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])<eps)continue;
for(int i = c;i<=n;i++)swap(a[r][i],a[t][i]);
for(int i = n;i>=c;i--)a[r][i]/=a[r][c];
for(int i = r+1;i<n;i++){
if(fabs(a[i][c])>eps){
for(int j = n;j>=c;j--){
a[i][j] -= a[i][c] * a[r][j];
}
}
}
r++;
}
if(r<n){
for(int i = r;i<n;i++){
if(fabs(a[i][n])>eps)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];
return 0;
}
int main(){
scanf("%d",&n);
for(int i = 0;i<n;i++)
for(int j = 0;j<=n;j++)scanf("%lf",&a[i][j]);
int t = gauss();
if(t==2)return puts("-1"),0;
if(t==1)return puts("0"),0;
for(int i = 0;i<n;i++){
cout<<"x"<<i+1<<"=";
printf("%.2lf\n",a[i][n]);
}
return 0;
}