在本教程中,我们将讨论一个程序来查找三个数字的公比。
为此,我们将获得a:b和b:c。我们的任务是找到这三个数字a:b:c的公比并打印出来。
#include <bits/stdc++.h> using namespace std; //计算a:b:c- void solveProportion(int a, int b1, int b2, int c) { int A = a * b2; int B = b1 * b2; int C = b1 * c; int gcd = __gcd(__gcd(A, B), C); cout << A / gcd << ":" << B / gcd << ":" << C / gcd; } int main() { int a, b1, b2, c; a = 3; b1 = 4; b2 = 8; c = 9; solveProportion(a, b1, b2, c); return 0; }
输出结果
6:8:9