扩展欧几里得算法
- 在openEuler(推荐)或Ubuntu或Windows(不推荐)中完成下面任务
- 参考《密码工程》p112伪代码实现ExtendedGCD(int a, int b, int *k, int *u, int *v)算法(10’)
- 在测试代码中计算74模167的逆。(5‘)
- 提交代码和运行结果截图
#include <stdio.h>
struct EX_GCD {
int u;
int v;
int k;
};
struct EX_GCD extended_euclidean(int a, int b,int k, int u, int v) {
struct EX_GCD ex_gcd;
if (b == 0) {
ex_gcd.u = 1;
ex_gcd.v = 0;
ex_gcd.k = 0;
return ex_gcd;
}
int old_r = a, r = b;
int old_u = 1, u1 = 0;
int old_v = 0, v1 = 1;
while (r != 0) {
int q = old_r / r;
int temp = old_r;
old_r = r;
r = temp - q * r;
temp = old_u;
old_u = u1;
u1 = temp - q * u1;
temp = old_v;
old_v = v1;
v1 = temp - q * v1;
}
ex_gcd.u = old_s;
ex_gcd.v = old_t;
ex_gcd.k = old_r;
return ex_gcd;
}
int main(void) {
int a, b;
printf("输入两个整数:\n");
scanf("%d%d", &a, &b);
if (a < b) {
int temp = a;
a = b;
b = temp;
}
struct EX_GCD s = extended_euclidean(a, b);
printf("%d*%d+%d*%d=%d\n", s.u, a, s.v, b, s.k);
printf("%d %d %d\n", s.u,s.v,s.k);
return 0;
}