欠了好久的高斯消元
#include<iostream> #include<cstdio> #include<cmath> using namespace std; const int MAXN=128; inline int rd(){ int ret=0,f=1;char c; while(c=getchar(),!isdigit(c))f=c=='-'?-1:1; while(isdigit(c))ret=ret*10+c-'0',c=getchar(); return ret*f; } double a[MAXN][MAXN],val[MAXN]; int n,w[MAXN]; bool gauss(){ double eps=1e-6; for(int i=1;i<=n;i++){ double mx=0;int pos=-1; for(int j=1;j<=n;j++){ if(fabs(a[i][j])>mx+eps){mx=fabs(a[i][j]);pos=j;} } if(pos==-1) return 0; w[i]=pos; for(int j=1;j<=n;j++){ if(j==i) continue; double t=a[j][pos]/a[i][pos]; for(int k=1;k<=n+1;k++) a[j][k]-=t*a[i][k]; } } for(int i=1;i<=n;i++) val[w[i]]=a[i][n+1]/a[i][w[i]]; return 1; } int main(){ n=rd(); for(int i=1;i<=n;i++) for(int j=1;j<=n+1;j++) a[i][j]=rd(); if(gauss()==0) return printf("No Solution"),0; for(int i=1;i<=n;i++) printf("%.2lf ",val[i]); return 0; }