Template-Gaussian elimination (Hihocoder 1195)

Source: Internet
Author: User
Tags mul

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;
 }


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.