Skip to content

Commit 3085f7b

Browse files
authored
拓展欧几里德算法
1 parent 41d99e0 commit 3085f7b

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

Extended-Euclid.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#include <cstdio>
2+
3+
using namespace std;
4+
5+
int extended_gcd(int a, int b, int &x, int &y)
6+
{
7+
if (b == 0)
8+
{
9+
x = 1;
10+
y = 0;
11+
return a;
12+
}
13+
int _gcd = extended_gcd(b, a % b, x, y);
14+
int temp = x;
15+
x = y;
16+
y = temp - (a / b) * y;
17+
return _gcd;
18+
}
19+
20+
int main()
21+
{
22+
int a, b, x, y;
23+
while (true)
24+
{
25+
scanf("%d%d", &a, &b);
26+
int gcd = extended_gcd(a, b, x, y);
27+
printf("gcd = %d, x = %d, y = %d.\n", extended_gcd(a, b, x, y), x, y);
28+
}
29+
30+
return 0;
31+
}

0 commit comments

Comments
 (0)