Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
发个小菜我自己写的高斯消元法,还请路过的大牛们不要见笑。#include<iostream> using namespace std; int move[9][9]={ //A,B,C,D,E,F,G,H,I {1,1,0,1,1,0,0,0,0},//move1 {1,1,1,0,0,0,0,0,0},//move2 {0,1,1,0,1,1,0,0,0},//move3 {1,0,0,1,0,0,1,0,0},//move4 {0,1,0,1,1,1,0,1,0},//move5 {0,0,1,0,0,1,0,0,1},//move6 {0,0,0,1,1,0,1,1,0},//move7 {0,0,0,0,0,0,1,1,1},//move8 {0,0,0,0,1,1,0,1,1} //move9 }; int NF=9;//所求方程组中方程的个数 //高斯消元法数据接口 int **F; int *Ans,*X; void Init(int N) { //int **F,*Ans,*X; int i; F=new int *[N]; for(i=0;i<N;i++) F[i]=new int[N]; Ans=new int[N]; X=new int[N]; } void Free(int N) { int i; for(i=0;i<NF;i++) delete[] F[i]; delete[] F; delete[] Ans; delete[] X; } int Guass(int N,int **f,int *ans,int *x); //////////////////////////////////////////// int main() { //freopen("input.txt","r",stdin); int i,j; int clocks[9]; for(i=0;i<NF;i++) cin>>clocks[i]; Init(NF); for(i=0;i<NF;i++){ for(j=0;j<NF;j++){ F[i][j]=move[j][i]; } Ans[i]=(4-clocks[i])%4; } Guass(NF,F,Ans,X); for(i=0;i<NF;i++) { X[i]=(X[i]%4+4)%4; } //for(i=0;i<NF;i++) cout<<X[i]<<' '; //cout<<endl; for(i=0;i<NF;i++){ for(j=1;j<=X[i];j++) cout<<i+1<<' '; } cout<<endl; Free(NF); return 0; } ///////////////////////////////////////////// int Guass(int N,int **f,int *ans,int *x) { int t; int i,j,k; bool flag; int temp; //int *temp=new int[N]; for(t=0;t<=N-1;t++)//将方程组化为阶梯矩阵 { if(f[t][t]==0) { flag=false; for(i=t+1;i<=N-1;i++){ if(f[i][t]!=0){ flag=true; for(k=0;k<=N-1;k++){ temp=f[t][k]; f[t][k]=f[i][k]; f[i][k]=temp; } temp=ans[t]; ans[t]=ans[i]; ans[i]=temp; break; } } if(!flag) return 1;//如果无法将原方程组矩阵化为阶梯矩阵则返回1,表示函数失败 } //temp=f[t][t]; for(i=t+1;i<=N-1;i++){ if(f[i][t]!=0){ temp=f[i][t]; for(j=t;j<=N-1;j++){ f[i][j]*=f[t][t]; f[i][j]-=(f[t][j]*temp); } ans[i]=ans[i]*f[t][t]-ans[t]*temp; } } }//end for t for(i=N-1;i>=0;i--){ temp=ans[i]; for(j=N-1;j>i;j--){ temp-=f[i][j]*x[j]; } //以下步骤为解同余类方程组情况下计算解的过程的必要变形 temp=(temp%4+4)%4; for(j=0;j<=3;j++) if((j*f[i][i]%4+4)%4==temp) x[i]=j; //x[i]=temp/f[i][i]; } return 0; } Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator