第十章课后练习:1、2、3、7
#include<iostream>
using namespace std;
int Inverse(int m, int b)
{
int k = 1;
for(;;)
{
if ((m * k + 1) % b == 0)
{
return (m * k + 1) / b;
break;
}
k++;
}
}
int main()
{
cout << "请依次输入同余方程个数及同余方程" << endl;
int num, M = 1;
int input[10], b[10], xb[10], ans = 0;
int Input[10];
string mod;
char x, e;
cin >> num;
for (int i = 0; i < num; i++)
{
cin >> x >> mod >> input[i] >> e >> Input[i];
}
for (int i = 0; i < num; i++)
{
M *= input[i];
}
for (int i = 0; i < num; i++)
{
b[i] = M / input[i];
}
for (int i = 0; i < num; i++)
{
xb[i] = Inverse(input[i], b[i]);
}
for (int i = 0; i < num; i++)
{
ans += Input[i] * b[i] * xb[i];
}
ans %= M;
cout << ans;
return 0;
}