В массиве A целых элементов из диапазона [-15;20] вычислить количество нечетных
элементов, которые находятся после максимального
C/C++
Составить программу на C++ если можно с объяснением.!
Считает количество нечётных элементов после первого максимального числа
// Массив заполняется случайными числами
#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;});
}
// Массив заполняется случайными числами
#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;});
}
#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. Кто учит, тот пусть и объясняет. Ему за это деньги платят.
#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. Кто учит, тот пусть и объясняет. Ему за это деньги платят.
Похожие вопросы
- Составить программу на c++ УСПОЛЬЗУЯ МАССИВ!!!
- ПОМОГИТЕ СОСТАВИТЬ ПРОГРАММУ НА C++.
- Помогите пожалуйста составить правильную программу на C++ 12 вариант
- Составить программу c++ срочно пожалуйста
- Помогите составить программу на Visual Studio C++
- Составить программу C++, с помощью switch/case
- Составить программу для вычисления значения выражения. C++
- Составить программу для выполнения задания c++, с циклом
- Помогите написать программу на C++
- Написать программу на c++