-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexercise1.cpp
More file actions
80 lines (69 loc) · 1.13 KB
/
Copy pathexercise1.cpp
File metadata and controls
80 lines (69 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <stdio.h>
#include <stdlib.h>
//简单乘法
int simple_multiply(int a, int b) {
int sum = 0;
int count = 0;
//考虑b为0的情况
if (b == 0) {
return 0;
}
//考虑负数的情况
int flag = b < 0;
if (flag) {
b = -b;
}
while (b != 0) {
int t = b & 0x01;
if (t == 1) {
sum += a << count;
}
count++;
b >>= 1;
}
return flag ? -sum : sum;
}
//二进制gcd
int binary_gcd(int a, int b) {
if (a == 0) {
return b;
}
if (b == 0) {
return a;
}
if (a % 2 == 0 && b % 2 == 0) {
return 2 * binary_gcd(a >> 1, b >> 1);
}
else if (a % 2 == 0) {
return binary_gcd(a >> 1, b);
}
else if (b % 2 == 0) {
return binary_gcd(a, b >> 1);
}
else {
int min = a < b ? a : b;
int max = a > b ? a : b;
return binary_gcd(max - min, min);
}
}
//二进制 egcd
int binary_egcd(int a, int b, int &x, int& y) {
int d = a;
if (d != 0) {
d = binary_egcd(b, a%b, y, x);
y = (y - (a / b)*y);
}
else {
x = 1;
y = 0;
}
return 0;
}
int main() {
//int a = -5, b = 0;
//int c = simple_multiply(a, b);
int b = 12345, a = 678;
int c = binary_gcd(a, b);
printf("%d\n", c);
system("pause");
}