C/C++

Составить программу на C++ если можно с объяснением.!

В массиве A целых элементов из диапазона [-15;20] вычислить количество нечетных
элементов, которые находятся после максимального
Считает количество нечётных элементов после первого максимального числа

// Массив заполняется случайными числами
#include <algorithm>
#include <iostream>
#include <vector>
#include <ctime>
using namespace std;

int main() {
int size = 0;
cin >> size;
vector<int> integers(size);
srand((time(0)));
for (int& item : integers) item = rand() % 36 - 15;

auto max_it = max_element(integers.begin(), integers.end());
cout << count_if(max_it + 1, integers.end(),
[](int value) {return value % 2 != 0;});
}

// Заполнение массива с консоли
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

int main() {
int size = 0;
cin >> size;
vector<int> integers(size);
for (int& item : integers) cin >> item;

auto max_it = max_element(integers.begin(), integers.end());
cout << count_if(max_it + 1, integers.end(),
[](int value) {return value % 2 != 0;});
}
Andro Andro
Andro Andro
6 243
Лучший ответ
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <random>
using namespace std;
int main() {
uniform_int_distribution<> uid(-15, 20);
mt19937 gen{ random_device()() };
auto rand = [&] { return uid(gen); };
auto output = [](int x) { cout << setw(4) << x; };
int box[18];
generate(begin(box), end(box), rand);
for_each(begin(box), end(box), output);
puts("");
auto max = max_element(begin(box), end(box));
auto index = max - begin(box);
cout
<< "Max: " << *max << '\n'
<< "Index: " << index << '\n';
if (max + 1 != end(box)) {
auto odd = [](int x) { return x & 1; };
auto n = count_if(max + 1, end(box), odd);
cout << "Count: " << n << '\n';
} else {
puts("Max == Last");
}
system("pause > nul");
}

P.S. Кто учит, тот пусть и объясняет. Ему за это деньги платят.