diff --git "a/\346\225\260\350\256\272\357\274\210GCD\345\222\214LCM\345\205\263\347\263\273\357\274\211" "b/\346\225\260\350\256\272\357\274\210GCD\345\222\214LCM\345\205\263\347\263\273\357\274\211" new file mode 100644 index 0000000..873e1c2 --- /dev/null +++ "b/\346\225\260\350\256\272\357\274\210GCD\345\222\214LCM\345\205\263\347\263\273\357\274\211" @@ -0,0 +1,60 @@ +#include +#include +#include + +using namespace std; + +// 函数:计算两个数的最大公约数 (GCD) +int gcd(int a, int b) { + while (b != 0) { + int temp = b; + b = a % b; + a = temp; + } + return a; +} + +int main() { + const int g = 12; // 已知的 gcd + const int l = 360; // 已知的 lcm + + // 检查输入是否合法:必须满足 g*l = a*b 的约束,但这里我们直接找 x*y = l/g + int product_xy = l / g; + + if (l % g != 0) { + cout << "Invalid input: LCM must be a multiple of GCD." << endl; + return 0; + } + + cout << "Finding all pairs (a, b) such that:" << endl; + cout << " gcd(a, b) = " << g << ", lcm(a, b) = " << l << endl; + cout << " a and b are positive integers, and a <= b" << endl << endl; + + vector> solutions; + + // 枚举所有 x*y = product_xy,且 gcd(x, y) == 1,x <= y + for (int x = 1; x * x <= product_xy; ++x) { + if (product_xy % x == 0) { + int y = product_xy / x; + + if (gcd(x, y) == 1) { + int a = g * x; + int b = g * y; + + if (a <= b) { + solutions.emplace_back(a, b); + } + } + } + } + + // 输出结果 + cout << "All possible (a, b) pairs are:" << endl; + for (const auto& p : solutions) { + cout << "(" << p.first << ", " << p.second << ")" << endl; + } + + cout << "\nTotal number of solutions: " << solutions.size() << endl; + + return 0; +}