C/C++

Напишите программу на С++

#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <vector>
#include <ctime>
using namespace std;
void print(vector <int> x)
{ int i, j = 0; for (i = 0; i < x.size(); i++)
{ cout << setw(5) << x[i]; j++; if (j == 10)
{ j = 0; cout << endl; } } if (j) cout << endl;
cout << endl; }
int main()
{ int i, n, max, imax = 0; cout << "n = ";
cin >> n; vector A(n), B;
srand(time(nullptr)); for (i = 0; i < n; i++)
A[i] = rand() % 1999 - 999;
cout << "Array A:" << endl;
print(A); max = A[0];
for (i = 1; i < n; i++) if (A[i] > max)
{ max = A[i]; imax = i; }
cout << "max = " << max <<", element №"
<< imax + 1 << endl << endl;
for (i = imax + 1; i < n; i++) if (A[i] > 0)
B.push_back(A[i]);
if (B.size()) { cout << "Array B:" << endl;
print(B); } }
SB
Seka_99_11 Baltabaev
66 572
Лучший ответ
Никиш Иванов Считаете лучше чем код из 4 строчек? Остальное переменные и инициализация
Seka_99_11 Baltabaev За максимальным? А у меня вроде перед первым максимальным.
Да пжалста =)

#include < iostream >
#include < random >
#include < algorithm >
#include < iterator >
#include < vector >
#include < functional >

using namespace std;

int main() {
const size_t size = 10;
const int gen_min = -99;
const int gen_max = 99;

mt19937 engine {random_device()()};
uniform_int_distribution< int > dist {gen_min, gen_max};
vector< int > vA(size); vector< int > vB;

generate(begin(vA), end(vA), bind(dist, engine));

cout << "A:\n"; copy(begin(vA), end(vA), ostream_iterator< int >(cout, " "));

copy_if(max_element(begin(vA), end(vA)), end(vA), back_inserter(vB), bind2nd(greater< int >(), 0));

cout << "\nB:\n"; copy(begin(vB), end(vB), ostream_iterator< int >(cout, " "));
}

https://onlinegdb.com/IYUVG3rU8

P.S: Если считаете ноль положительным вместо greater поставьте greater_equal
НИ
Никиш Иванов
84 764
Никиш Иванов ЗЫЖ чуть эффективнее было бы сделать создание второго вектора с размером distance(end - iterator от max) и обойтись без back_inserter. Хотя vector оптимизируется, но всё ещё возможна ситуация с перераспределением памяти.
Но для "домашки" думаю это несущественно. =)
А вот вывод сделать функцией тупо лень было =)