https://www.luogu.org/problemnew/show/P3389
给定一个线性方程组,对其求解
板子在此。
#include<algorithm> #include<iostream> #include<cstring> #include<cstdio> #include<cctype> #include<cmath> using namespace std; typedef double dl; const int N=103; inline int read(){ int X=0,w=0;char ch=0; while(!isdigit(ch)){w|=ch=='-';ch=getchar();} while(isdigit(ch))X=(X<<3)+(X<<1)+(ch^48),ch=getchar(); return w?-X:X; } dl f[N][N],ans[N]; inline bool gauss(int n,int m){ for(int i=1;i<=n;i++){ if(f[i][i]==0)return 0; int l=i; for(int j=i+1;j<=n;j++){ if(fabs(f[l][i])<fabs(f[j][i]))l=j; } if(l!=i){ for(int j=i;j<=m;j++) swap(f[i][j],f[l][j]); } for(int j=i+1;j<=n;j++){ dl t=f[j][i]/f[i][i]; for(int k=i;k<=m;k++) f[j][k]-=t*f[i][k]; } } for(int i=n;i>=1;i--){ for(int j=i+1;j<=n;j++) f[i][m]-=ans[j]*f[i][j]; ans[i]=f[i][m]/f[i][i]; } return 1; } int main(){ int n=read(); for(int i=1;i<=n;i++){ for(int j=1;j<=n+1;j++){ f[i][j]=read(); } } if(!gauss(n,n+1))puts("No Solution"); else{ for(int i=1;i<=n;i++){ printf("%.2lf\n",ans[i]); } } return 0; }
+++++++++++++++++++++++++++++++++++++++++++
+本文作者:luyouqi233。 +
+欢迎访问我的博客:http://www.cnblogs.com/luyouqi233/+
+++++++++++++++++++++++++++++++++++++++++++