Advertisement
vencinachev

Ex2.Recursion

Jan 16th, 2021
882
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.06 KB | None | 0 0
  1. #include <iostream>
  2. #include <bits/stdc++.h>
  3.  
  4. using namespace std;
  5.  
  6. const int SIZE = 100;
  7. int digitSum(int number);
  8. int arraySteps(int a[], int sp, int n);
  9.  
  10. int main()
  11. {
  12.     int n, spos;
  13.     int a[SIZE];
  14.     do
  15.     {
  16.         cout << "Enter n: ";
  17.         cin >> n;
  18.     }
  19.     while (n < 0 || n >= SIZE);
  20.  
  21.     do
  22.     {
  23.         cout << "Enter start position: ";
  24.         cin >> spos;
  25.     }
  26.     while (spos < 0 || spos >= n);
  27.  
  28.     cout << "Enter array: ";
  29.     for (int i = 0; i < n; i++)
  30.     {
  31.         cin >> a[i];
  32.     }
  33.     cout << "Steps count: " << arraySteps(a, spos, n) << endl;
  34.  
  35.     return 0;
  36. }
  37.  
  38. int digitSum(int number)
  39. {
  40.     if (number == 0)
  41.     {
  42.         return 0;
  43.     }
  44.     return (number % 10) + digitSum(number / 10);
  45. }
  46.  
  47. int arraySteps(int a[], int sp, int n)
  48. {
  49.     if (sp < 0 || sp >= n || a[sp] == INT_MIN)
  50.     {
  51.         return 0;
  52.     }
  53.     else if (digitSum(a[sp]) % 2 == 0)
  54.     {
  55.         a[sp] = INT_MIN;
  56.         return 1 + arraySteps(a, sp + 3, n);
  57.     }
  58.     a[sp] = INT_MIN;
  59.     return 1 + arraySteps(a, sp - 2, n);
  60. }
  61.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement