void gauss(double a[3][3], double b[], int m, double x[]) { for (int i=0; i<m; i++) { float tmp = a[i][i]; int t; //选主元 for (int k=i+1; k<m; k++) { if (fabs(tmp) < fabs(a[k][i])) { t=k; tmp = a[k][i]; } } //列主元(换行) for (int j=i; j<m; j++) { tmp=a[i][j]; a[i][j]=a[t][j]; a[t][j]=tmp; } tmp = b[i]; b[i] = b[t]; b[t] = tmp; //列消去 for(int k=i+1;k<m;k++) { a[k][i]=a[k][i]/a[i][i]; for(int j=i+1;j<m;j++) { a[k][j]=a[k][j]-a[k][i]*a[i][j]; } b[k]=b[k]-a[k][i]*b[i]; } } //回代 x[m-1]=b[m-1]/a[m-1][m-1]; for(int i=m-2;i>=0;i--) { for(int k=m-1;k>i;k--) { b[i]=b[i]-a[i][k]*x[k]; } x[i]=b[i]/a[i][i]; } }