CINTA作业2

gcd算法的迭代实现

int my_gcd(int a, int b) {//gcd算法迭代版本
	int temp;
	while (a % b != 0) {
		temp = a % b;
		a = b;
		b = temp;
	}
	return b;
}

egcd计算r和s

typedef struct Matrix {//定义矩阵类型
	int r, s, num;
	Matrix(int a=0, int b=0, int c=0) {
		r = a;s = b;num = c;
	}
	Matrix(const Matrix& M) {
		r = M.r;
		s = M.s;
		num = M.num;
	}
}Matrix;
void egcd(int a,int b) {
	Matrix M1{ 1,0,a };
	Matrix M2{ 0,1,b };
	Matrix temp;
	int d;
	while (M2.num != my_gcd(a, b)) {
		temp.num = M1.num % M2.num;
		d = M1.num / M2.num;
		temp.r = M1.r - d * M2.r;
		temp.s = M1.s - d * M2.s;
		M1 = M2;M2 = temp;
	}
	cout << "r=" << M2.r <<endl<< "s=" << M2.s << endl;
}

gcd批处理数组

int gcd_many(int arr[],int num) {
	int max_divisor=arr[0];
	for (int i = 0;i < num-1;i++) {
		max_divisor = my_gcd(max_divisor, arr[i + 1]);
	}
	return max_divisor;
}

Bezout的证明
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值