Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- // Вычисление НОД двух чисел
- long long gcd(long long a, long long b)
- {
- while (b != 0)
- {
- long long t = a % b;
- a = b;
- b = t;
- }
- return a;
- }
- // Вычисление НОК двух чисел
- long long lcm(long long a, long long b)
- {
- return (a / gcd(a, b)) * b;
- // важно (a/gcd(a,b))*b, чтобы избежать переполнения
- }
- int main()
- {
- int k;
- cout << "Сколько чисел: ";
- cin >> k;
- if (k <= 0)
- {
- cout << "Неверное k\n";
- return 0;
- }
- long long x;
- cin >> x; // первое число
- long long currentGCD = x;
- long long currentLCM = x;
- for (int i = 1; i < k; i++)
- {
- cin >> x;
- currentGCD = gcd(currentGCD, x);
- currentLCM = lcm(currentLCM, x);
- }
- cout << "НОД = " << currentGCD << endl;
- cout << "НОК = " << currentLCM << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement