В одномерном массиве, сладаеться из N действительных элементов, вычислить:
1) Максимальный по модулю элемент массива
2) Сумма элементов, которые располагаются между первым и вторым положительными элементами.
Перетвоиты массив таким образом, чтобы элементы, которые равны нулю и единице, располагались после всех остальных
Другие языки программирования и технологии
Задача в С++. Помогите.
#include <iostream>
#include <vector>
using namespace std;
using box_t = pair<int*, const size_t>;
int abs_max(const box_t& box) {
auto[seq, len] = box;
auto max = 0;
auto index = 0U;
for (auto i = 0U; i < len; ++i) {
const auto next = abs(seq[i]);
if (next > max) {
max = next;
index = i;
}
}
return seq[index];
}
long long sum_between(const box_t& box) {
auto[seq, len] = box;
auto sum = 0LL;
auto i = 0U;
while (i < len && seq[i] < 0) ++i;
while (++i < len && seq[i] < 0) sum += seq[i];
if (i == len) sum = 0;
return sum;
}
void transform(box_t& box) {
auto[seq, len] = box;
vector<int> tmp;
for (auto i = 0U; i < len; ++i) if (seq[i] != 0 && seq[i] != 1) tmp.push_back(seq[i]);
for (auto i = 0U; i < len; ++i) if (seq[i] == 0 || seq[i] == 1) tmp.push_back(seq[i]);
for (auto i = 0U; i < len; ++i) seq[i] = tmp.at(i);
}
void show(const box_t& box) {
auto[seq, len] = box;
for (auto i = 0U; i < len; ++i) cout << ' ' << seq[i];
cout.put('\n');
}
int main() {
int seq[] = { -3, -7, 3, -5, -7, -6, -1, 4, 1, -9, 0, -4, 1, 1, 1, -7, 0, 6 };
box_t box{ seq, size(seq) };
cout << "Array:";
show(box);
const auto am = abs_max(box);
cout << "Max: " << am << '\n';
const auto sb = sum_between(box);
cout << "Sum: " << sb << '\n';
transform(box);
cout << "Array:";
show(box);
system("pause");
}
#include <vector>
using namespace std;
using box_t = pair<int*, const size_t>;
int abs_max(const box_t& box) {
auto[seq, len] = box;
auto max = 0;
auto index = 0U;
for (auto i = 0U; i < len; ++i) {
const auto next = abs(seq[i]);
if (next > max) {
max = next;
index = i;
}
}
return seq[index];
}
long long sum_between(const box_t& box) {
auto[seq, len] = box;
auto sum = 0LL;
auto i = 0U;
while (i < len && seq[i] < 0) ++i;
while (++i < len && seq[i] < 0) sum += seq[i];
if (i == len) sum = 0;
return sum;
}
void transform(box_t& box) {
auto[seq, len] = box;
vector<int> tmp;
for (auto i = 0U; i < len; ++i) if (seq[i] != 0 && seq[i] != 1) tmp.push_back(seq[i]);
for (auto i = 0U; i < len; ++i) if (seq[i] == 0 || seq[i] == 1) tmp.push_back(seq[i]);
for (auto i = 0U; i < len; ++i) seq[i] = tmp.at(i);
}
void show(const box_t& box) {
auto[seq, len] = box;
for (auto i = 0U; i < len; ++i) cout << ' ' << seq[i];
cout.put('\n');
}
int main() {
int seq[] = { -3, -7, 3, -5, -7, -6, -1, 4, 1, -9, 0, -4, 1, 1, 1, -7, 0, 6 };
box_t box{ seq, size(seq) };
cout << "Array:";
show(box);
const auto am = abs_max(box);
cout << "Max: " << am << '\n';
const auto sb = sum_between(box);
cout << "Sum: " << sb << '\n';
transform(box);
cout << "Array:";
show(box);
system("pause");
}
Похожие вопросы
- Задача Pascal , строки , помогите пожалуйста !!!
- Простая задача на VBA ! Помогите советом ...
- задачу на паскале помогите Завершить!
- Как решить эти 3 задачи на С++? Помогите, пожалуйста.
- Задачи в С++,помогите прошу
- олимпиадная задача по информатике. Помогите решить пожалуйста
- задача по ассемблеру.помогите!
- Люди мне надо решить задачи по информатике помогите. За верную помощь стикер*
- Задача в Pascal Помогите решить
- Задача по Pascal Помогите, пожалуйта.