C/C++

Задачи на C++

Вывести на экран массив.Найдите наименьший четный элемент массива. Если такого нет, то выведите первый элемент
OR
Oleh Rrrrrrr
215
#include <algorithm>
#include <iostream>
#include <array>
#include <random>
using namespace std;
int main() {
array<int, 10> box;
uniform_int_distribution<> uid(10, 99);
mt19937 gen{ random_device()() };
for (auto& x : box) x = uid(gen);
for (auto x : box) cout << x << ' ';
puts("");
auto tmp = box;
sort(tmp.begin(), tmp.end());
auto even = [](int x) { return 0 == x % 2; };
auto pos = find_if(tmp.begin(), tmp.end(), even);
if (pos != box.end()) cout << "min even: " << *pos << '\n';
else cout << "first element: " << box.front() << '\n';
}
Николай Герасимов
Николай Герасимов
79 892
Лучший ответ
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
using namespace std; int main()
{ int i, j = 0, k = 0, min = 1000, n; cout << "n: ";
cin >> n; int *array = new int [n];
srand(time(nullptr)); for (i = 0; i < n; i++)
{ ++j; array[i] = rand() % 1999 - 999;
cout << setw(5) << array[i];
if (array[i] < min && array[i] % 2 == 0)
{ min = array[i]; ++k; } if (j == 10)
{ j = 0; cout << endl; } } if (j) cout << endl;
if (k) cout << endl << min << endl;
else cout << endl << array[0] << endl;
delete [] array; system("pause");
return 0; }
Александр Дука
Александр Дука
66 572