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
| #include <iostram> using namespace std; int gcd(int x, int y) { int k = 1; while (x && y) { if (x % 2 == 1 || y % 2 == 1) { if (x > y) { swap(x, y); } y -= x; } else if (x % 2 == 0 && y % 2 == 0) { k *= 2; x /= 2; y /= 2; } } return max(x, y) * k; } int lcm(int x, int y) { return x / gcd(x, y) * y; } int main() { int a, b; cin >> a >> b; cout << gcd(a, b) << endl; cout << lcm(a, b) << endl; return 0; }
|