【高斯消元】CDOJ1785 曜酱的线性代数课堂(三)
高斯消元求行列式板子。
#include<cstdio> #include<cmath> #include<algorithm> #include<cstring> using namespace std; const double EPS=0.00000001; #define N 105 int n; double B[N][N],A[N][N]; double guass_jordan() { double res=1.0; 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(i!=pivot){ res*=(-1.0); } if(fabs(B[i][i])<EPS){ return 0.0; } for(int j=i+1;j<=n;++j) B[i][j]/=B[i][i]; res*=B[i][i]; for(int j=1;j<=n;++j) if(i!=j) for(int k=i+1;k<=n;++k) B[j][k]-=B[j][i]*B[i][k]; } return res; } int main() { // freopen("hanglieshi3.in","r",stdin); // freopen("hanglieshi3.out","w",stdout); char t[1000]; while(scanf("%d",&n)!=EOF){ memset(A,0,sizeof(A)); for(int i=1;i<=n;++i){ for(int j=1;j<=n;++j){ scanf("%lf",&A[i][j]); } } double ans=guass_jordan(); sprintf(t,"%.3f",ans); if(t[0]=='-' && t[1]=='0' && t[2]=='.' && t[3]=='0' && t[4]=='0' && t[5]=='0'){ puts("0.000"); } else{ printf("%.3f\n",ans); } } return 0; }
——The Solution By AutSky_JadeK From UESTC
转载请注明出处:http://www.cnblogs.com/autsky-jadek/