mjc24268 @ 2019-08-22 15:12:30
90分 用的是高斯_约旦消元
#include<bits/stdc++.h>
using namespace std;
#define eps 1e-8
double a[105][105];
int n;
int main(){
scanf("%d",&n);
for(int i=1;i<=n;i++){
for(int j=1;j<=n+1;j++){
scanf("%lf",&a[i][j]);
}
}
for(int i=1;i<=n;i++){
int maxx=i;
for(int j=i+1;j<=n;j++){
if(fabs(a[j][i])-fabs(a[maxx][i])>=eps){
maxx=j;
}
if(fabs(fabs(a[j][i])-fabs(a[maxx][i]))<eps){
for(int k=i+1;k<=n;k++){
if((fabs(a[j][k]))<fabs(a[maxx][k])){
maxx=j;
}
}
}
}
for(int j=1;j<=n+1;j++){
swap(a[i][j],a[maxx][j]);
}
if(fabs(a[i][i])<eps){
if(fabs(a[i][n+1])>eps)
printf("-1");
else printf("0");
return 0;
}
for(int j=1;j<=n;j++){
if(j!=i){
double tmp=a[j][i]/a[i][i];
for(int k=i+1;k<=n+1;k++){
a[j][k]-=tmp*a[i][k];
}
}
}
}
for(int i=1;i<=n;++i){
if(fabs((a[i][n+1])/a[i][i])<=eps){
printf("x%d=0\n",i);
}
else
printf("x%d=%.2lf\n",i,a[i][n+1]/a[i][i]);
}
}
by PerfectPeter @ 2019-11-05 17:20:54
应该是被这种数据卡了
2
0 2 3
0 0 0
by PerfectPeter @ 2019-11-05 17:23:18
std:0
@mjc24268你输出的好像是-1