C/C++
Написать программу на C++
Дан массив A[n]. Напишите программу, которая записывает все отрицательные элементы, предшествующие максимальному элементу массива А – в массив B
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <random>
using namespace std;
void show(int* box, const size_t n, const streamsize w = 1U) {
for (auto i = 0U; i < n; ++i) cout << ' ' << box[i];
puts("");
}
int main() {
uniform_int_distribution<> uid(-99, 99);
mt19937 gen{ random_device()() };
auto rand = [&] { return uid(gen); };
cout << "n: ";
size_t n;
cin >> n;
auto a = new int[n];
generate(a, a + n, rand);
show(a, n, 7U);
auto pos = max_element(a, a + n);
cout << "max: " << *pos << '\n';
auto neg = [](int x) { return x < 0; };
const auto m = count_if(pos + 1, a + n, neg);
if (m) {
auto b = new int[m];
auto i = 0U;
for (auto it = pos + 1; it < a + n; ++it) {
if (neg(*it)) {
b[i] = *it;
++i;
}
}
show(b, m, 7U);
delete[] b;
} else {
puts("Not found!");
}
delete[] a;
system("pause > nul");
}
#include <iostream>
#include <iomanip>
#include <random>
using namespace std;
void show(int* box, const size_t n, const streamsize w = 1U) {
for (auto i = 0U; i < n; ++i) cout << ' ' << box[i];
puts("");
}
int main() {
uniform_int_distribution<> uid(-99, 99);
mt19937 gen{ random_device()() };
auto rand = [&] { return uid(gen); };
cout << "n: ";
size_t n;
cin >> n;
auto a = new int[n];
generate(a, a + n, rand);
show(a, n, 7U);
auto pos = max_element(a, a + n);
cout << "max: " << *pos << '\n';
auto neg = [](int x) { return x < 0; };
const auto m = count_if(pos + 1, a + n, neg);
if (m) {
auto b = new int[m];
auto i = 0U;
for (auto it = pos + 1; it < a + n; ++it) {
if (neg(*it)) {
b[i] = *it;
++i;
}
}
show(b, m, 7U);
delete[] b;
} else {
puts("Not found!");
}
delete[] a;
system("pause > nul");
}
#include "iostream"
#include "algorithm"
#include "cstdlib"
#include "ctime"
using namespace std;
int main (){
int n; cout<<"n: "; cin>>n; srand(time(NULL)); int *a=new int[n];
generate(a,a+n,[n]()mutable{return cout<<(n=rand()%199-99)<<' ',n;});
int k=0,*t=max_element(a,a+n); cout<< endl;
int m=count_if(a,t,[](int i){return i<0;}), *b=new int[m];
for_each(a,t,[&](int i){if(i<0)b[k++]=i;});
for_each(b,b+m,[](int i){cout<< i<<' ';});
delete []a; delete []b;}
#include "algorithm"
#include "cstdlib"
#include "ctime"
using namespace std;
int main (){
int n; cout<<"n: "; cin>>n; srand(time(NULL)); int *a=new int[n];
generate(a,a+n,[n]()mutable{return cout<<(n=rand()%199-99)<<' ',n;});
int k=0,*t=max_element(a,a+n); cout<< endl;
int m=count_if(a,t,[](int i){return i<0;}), *b=new int[m];
for_each(a,t,[&](int i){if(i<0)b[k++]=i;});
for_each(b,b+m,[](int i){cout<< i<<' ';});
delete []a; delete []b;}
Похожие вопросы
- Помогите написать программу на C++
- Написать программу на c++
- Помоги пожалуйста написать программу на C++
- Написать программу на C++.Создать класс vector3d, задаваемый тройкой координат. Создать конструктор...
- Помогите написать программу на C++
- Написать программу на C/C++. Найти количество отрицательных элементов под побочной диагональю.
- Помогите написать программу на C++
- ПОМОГИТЕ НАПИСАТЬ ПРОГРАММУ НА C++ ИСПОЛЬЗУЯ ТОЛЬКО ЦИКЛЫ.
- Написать программу на c++ с готовым кодом и блок схемой
- ПОМОГИТЕ НАПИСАТЬ ПРОГРАММУ НА C++