Gaussian elimination of the naked question, save as a template bar. It is worth noting that the accuracy of 1e-8, 1e-10, but 1e-6 can pass, it is said that each time to find the largest row elimination can reduce the error.
#include <iostream> #include <cstring> #include <cstdio> #include <queue> #include <
algorithm> #include <stdlib.h> #include <math.h> #include <stack> using namespace std; Const double EPS = 1e-6;
Keng!
BOOL Equal (double a,double b) {return (Fabs (a) <eps);
int n,m;
Double a[1010][1010];
Double b[1010];
Double ans[1010];
void Swap (int i,int j) {for (int k=1;k<=n;k++) {Swap (a[i][k],a[j][k]);
} swap (b[i],b[j]); } int main () {while (cin>>n>>m) {for (int. i=1;i<=m;i++) {for (int j=1;j<=n;j++) {Cin>>a[i]
[j];
} cin>>b[i];
} bool Mul=0;
for (int i=1;i<=n;i++) {//Processing column I bool flag = false; for (int j=i;j<=m;j++) {if (! Equal (a[j][i],0.0)) {//Finds row J if (I!=j) Swap (j,i) not 0 in column I;
Exchange IJ two lines of flag=1;
Break
}} if (!flag) {//If no such line is found mul=1;
Break } for (int j=i+1;j<=m;j++) {//i+1 row ~m row i column empty double tMP = A[j][i]/a[i][i];
for (int k=1;k<=n;k++) {//handling a[j][k] a[j][k]-=a[i][k]*tmp;
}//processing b[j] b[j]-=b[i]*tmp;
}} bool no=0;
for (int i=1;i<=m;i++) {bool flag=0; if (! Equal (b[i],0)) {for (int j=1;j<=n;j++) {if (!
Equal (a[i][j],0)) flag=1;
} if (!flag) {no=1;
}}} if (no) {printf ("no solutions\n");
Continue
} if (Mul) {printf ("many solutions\n");
Continue
} for (int i=n;i>=1;i--) {for (int j=i+1;j<=n;j++) {b[i]-=a[i][j]*ans[j];
a[i][j]=0;
} Ans[i]=b[i]/a[i][i];
} for (int i=1;i<=n;i++) {printf ("%d\n", (int) (ans[i]+eps));
}} return 0;
}