C++ - 扩展欧几里德算法非递归实现

#include 
using namespace std;
int x, y;
void get_x_y(int a, int b){
	int q, r[3], s[3], t[3];
	if(a < b)
		a ^= b ^= a ^= b;
	r[0] = a;
	r[1] = b;
	s[0] = 1;
	s[1] = 0;
	t[0] = 0;
	t[1] = 1;
	while(r[1] > 0){
		q = r[0] / r[1];
		r[2] = r[0] - q * r[1];
		s[2] = s[0] - q * s[1];
		t[2] = t[0] - q * t[1];
		r[0] = r[1];
		s[0] = s[1];
		t[0] = t[1];
		r[1] = r[2];
		s[1] = s[2];
		t[1] = t[2];
	}
	x = s[0];
	y = t[0];
	return;
}
int main(){
	int a, b;
	cin >> a >> b;
	get_x_y(a, b);
	cout << x << " " << y << endl;
	return 0;
}

你可能感兴趣的:(C++)