I and OI
Past...
#include<cmath>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<utility>
#include<iostream>
#include<algorithm>
using namespace std;

class equation
{
	private:
	double a[200][200];
	int i,j,k,r,c;
	double t;
	bool f;
	public:
	int e,v;
	double x[200];
	equation()
	{
		memset(a,0,sizeof(a));
		memset(x,0,sizeof(x));
	}
	void input()
	{
		scanf("%d %d\n",&e,&v);
		for (i=1;i<=e;i++)
		{
			for (j=1;j<=v+1;j++) scanf("%lf",&a[i][j]);
			scanf("\n");
		}
	}
	bool zero(double x){ return fabs(x)<=1e-8?1:0; }
	int solve()
	{
		for (r=1,c=1;r<=e && c<=v;r++,c++)
		{
			for (k=r,i=r+1;i<=e;i++) if (fabs(a[i][c])>fabs(a[k][c])) k=i;
			if (k!=r) for (i=1;i<=v+1;i++) swap(a[k][i],a[r][i]);
			if (zero(a[r][c])) { r--; continue; }
			for (i=r+1;i<=e;i++)
			if (!zero(a[i][c]))
			{
				t=a[i][c]/a[r][c];
				for(j=1;j<=v+1;j++) a[i][j]-=a[r][j]*t;
			}
		}
		for (i=1;i<=e;i++)
		{
			for (f=1,j=1;j<=v;j++) if (!zero(a[i][j])) f=0;
			if (f && !zero(a[i][v+1])) return -1;
		}
		if (r<=v) return v-r+1;
		for (i=v;i>=1;i--)
		{
			for (t=a[i][v+1],j=i+1;j<=v;j++) t-=a[i][j]*x[j];
			x[i]=t/a[i][i];
		}
		return 0;
	}
	void output()
	{
		for (i=1;i<=v;i++) printf("%.4lf ",x[i]);
		printf("\n");
	}
};

equation p;

int main()
{
	freopen("guass.in","r",stdin);
	freopen("guass.out","w",stdout);
	
	
	p.input();
	printf("%d\n",p.solve());
	p.output();	
	
		
}

  

posted on 2012-11-09 07:50  exponent  阅读(206)  评论(0编辑  收藏  举报