Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <bits/stdc++.h>
- using namespace std;
- const int SIZE = 100;
- int digitSum(int number);
- int arraySteps(int a[], int sp, int n);
- int main()
- {
- int n, spos;
- int a[SIZE];
- do
- {
- cout << "Enter n: ";
- cin >> n;
- }
- while (n < 0 || n >= SIZE);
- do
- {
- cout << "Enter start position: ";
- cin >> spos;
- }
- while (spos < 0 || spos >= n);
- cout << "Enter array: ";
- for (int i = 0; i < n; i++)
- {
- cin >> a[i];
- }
- cout << "Steps count: " << arraySteps(a, spos, n) << endl;
- return 0;
- }
- int digitSum(int number)
- {
- if (number == 0)
- {
- return 0;
- }
- return (number % 10) + digitSum(number / 10);
- }
- int arraySteps(int a[], int sp, int n)
- {
- if (sp < 0 || sp >= n || a[sp] == INT_MIN)
- {
- return 0;
- }
- else if (digitSum(a[sp]) % 2 == 0)
- {
- a[sp] = INT_MIN;
- return 1 + arraySteps(a, sp + 3, n);
- }
- a[sp] = INT_MIN;
- return 1 + arraySteps(a, sp - 2, n);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement