大概就是对每一行先找到最大的减小误差,然后代入消元
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cstdlib> 5 #include<cmath> 6 #include<algorithm> 7 #include<queue> 8 #include<vector> 9 #include<map> 10 #include<set> 11 #define ll long long 12 #define db double 13 #define inf 2139062143 14 #define MAXN 200100 15 #define rep(i,s,t) for(register int i=(s),i##__end=(t);i<=i##__end;++i) 16 #define dwn(i,s,t) for(register int i=(s),i##__end=(t);i>=i##__end;--i) 17 #define ren for(register int i=fst[x];i;i=nxt[i]) 18 #define Fill(x,t) memset(x,t,sizeof(x)) 19 #define pls(a,b) (a+b)%MOD 20 #define mns(a,b) (a-b+MOD)%MOD 21 #define mul(a,b) (1LL*(a)*(b))%MOD 22 using namespace std; 23 inline int read() 24 { 25 int x=0,f=1;char ch=getchar(); 26 while(!isdigit(ch)) {if(ch=='-') f=-1;ch=getchar();} 27 while(isdigit(ch)) {x=x*10+ch-'0';ch=getchar();} 28 return x*f; 29 } 30 int n;db a[110][110]; 31 void Gauss() 32 { 33 int pos;rep(i,0,n-1) 34 { 35 pos=n;rep(j,i,n-1)if(fabs(a[j][i])>fabs(a[pos][i])) pos=j; 36 if(pos==n) {puts("No Solution");exit(0);} 37 if(pos!=i) rep(j,0,n) swap(a[i][j],a[pos][j]); 38 dwn(j,n,i) rep(k,i+1,n-1) a[k][j]-=a[k][i]/a[i][i]*a[i][j]; 39 } 40 dwn(i,n-1,0) {rep(j,i+1,n-1) a[i][n]-=a[j][n]*a[i][j];a[i][n]/=a[i][i];} 41 } 42 int main() 43 { 44 n=read();rep(i,0,n-1) rep(j,0,n) a[i][j]=read();Gauss(); 45 rep(i,0,n-1) printf("%.2lf ",a[i][n]); 46 }