Sorry, your browser cannot access this site
This page requires browser support (enable) JavaScript
Learn more >

解方程的。。。

My code works but why?

#include<bits/stdc++.h>
#define fo(i,j,k) for(int i=j;i<=k;++i)
using namespace std;

const int MAXN=105;
int n;

struct juzhen{
	juzhen(int han,int lin){
		h=han,l=lin;
		memset(v,0,sizeof(v));	
	}
	
	juzhen(){
		memset(v,0,sizeof(v));			
	}
	
	void resize(int han,int lin){
		h=han,l=lin;
		memset(v,0,sizeof(v));	
	}
	
	
	void cleanForPow(void){
		memset(v,0,sizeof(v));
		int p=min(h,l);
		for(int i=1;i<=p;++i){
			v[i][i]=1;
		}
	}
	
	friend juzhen operator *(juzhen a,juzhen b){
		juzhen c(a.h,b.l);
		if(a.l!=b.h)return c;
		
		for(int i=1;i<=a.h;++i){
			for(int j=1;j<=b.l;++j){
				long long s=0;
				for(int k=1;k<=a.l;++k){
					s=s+(a.v[i][k]*b.v[k][j]);
				}
				c.v[i][j]=s;
			}
		}
		return c;
	}
	
	friend juzhen operator +(juzhen a,juzhen b){
		int ha=max(a.h,b.h);
		int li=max(a.l,b.l);
		juzhen c(ha,li);
		fo(i,1,ha){
			fo(j,1,li){
				c.v[i][j]=a.v[i][j]+b.v[i][j];
			}
		}
	}
	
	juzhen pow(int k){
		juzhen res=*this;
		juzhen ret(h,l);
		ret.cleanForPow();
		while(k){
			if(k&1){
				ret=ret*res;
			}
			res=res*res;
			k>>=1;
		}
		return ret;
	}
	
	void setV(long long t[MAXN][MAXN]){
		for(int i=1;i<=h;++i){
			for(int j=1;j<=l;++j){
				v[i][j]=t[i][j];
			}
		}
	}
	
	void print(void){
		fo(i,1,h){
			fo(j,1,l){
				cout<<v[i][j]<<' ';
			}
			cout<<'\n';
		}
	}
	
	void gsxy(void){
		for(int i=1;i<=n;++i){
			int mark=0;
			double tmp=0;
			for(int j=i;j<=n;++j){
				if(fabs(v[j][i])>tmp){
					mark=j;
					tmp=fabs(v[j][i]);
				}
			}
			for(int j=i;j<=n+1;j++){
				swap(v[i][j],v[mark][j]);
			}
			for(int j=1;j<=n;++j){
				if(i==j)continue;
				double rate=v[j][i]/v[i][i];
				for(int k=i;k<=n+1;++k){
					v[j][k]-=v[i][k]*rate;
				}
			}
		}
	}
	
	double v[MAXN][MAXN];
	int h,l;
};

int main(void){
	cin>>n;
	juzhen a(n,n+1);
	for(int i=1;i<=n;++i){
		for(int j=1;j<=n+1;++j){
			cin>>a.v[i][j];
		}
	}
	a.gsxy();
	
	for(int i=1;i<=n;++i){
		if(fabs(a.v[i][n+1]/a.v[i][i]<=1e-8))printf("0 ");
		else printf("%0.lf ",a.v[i][n+1]/a.v[i][i]);
	}
	return 0;
}

评论