Advertisement
vencinachev

K2Zad2

Dec 15th, 2020
859
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.85 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int gcd(int a, int b);
  6.  
  7. int func(int* arr, int n);
  8.  
  9. int main()
  10. {
  11.     int n;
  12.     cout << "Enter n: ";
  13.     cin >> n;
  14.     int a[n];
  15.     cout << "Enter " << n << " numbers: ";
  16.     for (int i = 0; i < n; i++)
  17.     {
  18.         cin >> a[i];
  19.     }
  20.     cout << "Result: " << func(a, n) << endl;
  21.     return 0;
  22. }
  23.  
  24. int gcd(int a, int b) {
  25.     while (b != 0) {
  26.         a %= b;
  27.         swap(a, b);
  28.     }
  29.     return a;
  30. }
  31.  
  32.  
  33. int func(int* arr, int n)
  34. {
  35.     int m = 0;
  36.     for (int i = 0; i < n; i++)
  37.     {
  38.         if (arr[i] >= 2)
  39.         {
  40.             arr[m] = arr[i];
  41.             m++;
  42.         }
  43.     }
  44.     n = m;
  45.  
  46.     if (n == 0)
  47.     {
  48.         return 0;
  49.     }
  50.     int gcdNum = arr[0];
  51.     for (int i = 0; i < n; i++)
  52.     {
  53.         gcdNum = gcd(arr[i], gcdNum);
  54.     }
  55.     return gcdNum;
  56. }
  57.  
  58.  
  59.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement