【高斯消元】CDOJ1784 曜酱的线性代数课堂(二)
高斯消元求矩阵秩板子。
#include<cstdio> #include<cmath> #include<algorithm> #include<cstring> using namespace std; const double EPS=0.00000001; #define N 105 int n,m; double B[N][N],A[N][N]; int guass_jordan() { memcpy(B,A,sizeof(A)); for(int i=1;i<=n;++i) { int pivot=i; for(int j=i+1;j<=n;++j) if(fabs(B[j][i])>fabs(B[pivot][i])) pivot=j; swap(B[i],B[pivot]); if(fabs(B[i][i])<EPS){ return i-1; } for(int j=i+1;j<=m;++j) B[i][j]/=B[i][i]; for(int j=1;j<=n;++j) if(i!=j) for(int k=i+1;k<=m;++k) B[j][k]-=B[j][i]*B[i][k]; } return n; } int main() { while(scanf("%d%d",&n,&m)!=EOF){ memset(A,0,sizeof(A)); for(int i=1;i<=n;++i){ for(int j=1;j<=m;++j){ scanf("%lf",&A[i][j]); } } printf("%d\n",guass_jordan()); } return 0; }
——The Solution By AutSky_JadeK From UESTC
转载请注明出处:http://www.cnblogs.com/autsky-jadek/