Advertisement
palenda21

Lab10A

Mar 23rd, 2020
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.83 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int sumr(int* mass, int i, int j)
  5. {
  6.     if (i == j && mass[i] > 0)
  7.     {
  8.         return mass[i];
  9.     }
  10.     else if (i == j)
  11.     {
  12.         return 0;
  13.     }
  14.    
  15.     else
  16.         return sumr(mass, i, i + (j - i) * 1 / 3) + sumr(mass, i + (j - i) * 1 / 3 + 1, j);
  17. }
  18. int main()
  19. {
  20.     cout << "Vvedite razmer massiva: ";
  21.     int n;
  22.     cin >> n;
  23.     int* mass = new int[n];
  24.     cout << "Vvedite elementi massiva: ";
  25.     for (int i = 0; i < n; i++)
  26.     {
  27.         cin >> mass[i];
  28.     }
  29.     cout << "Summa + elementov rekursivno: " << sumr(mass, 0, n - 1) << endl;
  30.     int s = 0;
  31.     for (int i = 0; i < n; i++)
  32.     {
  33.         if (mass[i] > 0)
  34.         {
  35.             s += mass[i];
  36.         }
  37.     }
  38.     cout << "Summa + elementov nerekursivno: " << s << endl;
  39.     delete[] mass;
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement