漫丁同学吧 关注:9贴子:300
  • 0回复贴,共1

解同余式组——利用中国剩余定理

只看楼主收藏回复

#include<iostream>
using namespace std;
int main (){
loop:int geshu,n,p;
cout<<"请输入方程个数:";
cin>>geshu;
int M;
int M1[geshu];
int M2[geshu];
int MZ[geshu];
int b[geshu];
int m[geshu];
int i=0;
for(i=0;i<geshu;i++){
cout<<"请输入第"<<i+1<<"个方程:x=b(mod m)"<<endl;
cout<<"b=";
cin>>b[i];
cout<<"m=";
cin>>m[i];
}
for(M=m[0],i=1;i<geshu;i++){
M=M*m[i];
}
for(i=0;i<geshu;i++){
M1[i]=M/m[i];
M2[i]=M1[i]%m[i];
MZ[i]=M1[i]*M2[i];
}
for(n=MZ[0],i=1;i<geshu;i++){
n=n+MZ[i];
}
p=n%M;
cout<<"x="<<p<<"(mod "<<M<<")\n"<<endl;
goto loop;
return 0;
}


1楼2013-12-23 22:36回复