高斯消元法【模板】 发表于 2017-06-30 基本上到要用的时候手画一下矩阵就推得出 以第i行的方程式消第i元如果模板元(a[i][i])为0,方程组存在多解回元的时候从n到i将每元从s中减去 CODE123456789101112131415161718192021222324252627282930313233343536373839404142434445#include<cstdio>#include<cstdlib>#include<cstring>#include<iostream>#include<cmath>using namespace std;double a[1010][1010],s[1010];double ans[1010]; int n; int main(){ scanf("%d",&n); for (int i=1;i<=n;++i) { for (int j=1;j<=n;++j) scanf("%lf",&a[i][j]); scanf("%lf",&s[i]); } for (int i=1;i<=n;++i) { if (fabs(a[i][i])<0.00000001) { printf("No Solution\n"); return 0; } for (int j=i+1;j<=n;++j) { double ch=a[j][i]/a[i][i]; for (int k=i;k<=n;++k) a[j][k]-=a[i][k]*ch; s[j]-=ch*s[i]; } } for (int i=n;i>=1;--i) { ans[i]=s[i]/a[i][i]; for (int j=1;j<i;++j) s[j]-=a[j][i]*ans[i]; } for (int i=1;i<=n;++i) printf("%.2lf\n",ans[i]); return 0;} --------------------------