Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Для нахождения наибольшего общего делителя в C++ есть удобная функция gcd из заголовочного файла <numeric>.
- #include <iostream>
- using namespace std;
- int main() {
- int a, b;
- cin >> a >> b;
- // Числа a и b должны быть натуральными
- while (b != 0) {
- int c = b;
- b = a % b;
- a = c;
- }
- cout << a << endl;
- }
- *******************************************************************************************************************
- #include <iostream>
- using namespace std;
- int GreatestCommonDivisor(int a, int b)
- {
- while (a > 0 && b > 0)
- {
- if (a > b)
- {
- a %= b;
- }
- else
- {
- b %= a;
- }
- }
- return a + b;
- }
- int main()
- {
- cout << GreatestCommonDivisor(10, 15);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement