给定一个线性方程组,对其求解
板子在此。
#include#include #include #include #include #include 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]) =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。 +
+欢迎访问我的博客:
+++++++++++++++++++++++++++++++++++++++++++