#include <iostream>
#include <vector>
#include <numeric>
int main() {
std::vector<int> D = {3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 9, 7, 9, 3, 2, 3, 8, 4, 6, 2, 6, 4, 3, 3};
int n = D.size(), totalSum = std::accumulate(D.begin(), D.end(), 0);
int maxSum = totalSum, maxIndex = 0, currentSum = totalSum;
for (int i = 1; i < n; ++i) {
currentSum = currentSum - D[i - 1] + D[i - 1];
if (currentSum > maxSum) {
maxSum = currentSum;
maxIndex = i;
}
}
std::cout << "Массив: ";
for (int num : D) std::cout << num << " ";
std::cout << "\nЭлемент с максимальной суммой: " << D[maxIndex] << "\nНаибольшая сумма: " << maxSum << std::endl;
return 0;
}
#include <array>
#include <iostream>
#include <random>
using namespace std;
size_t length(const char* prompt, size_t lim) {
size_t value = 0;
while (!value || lim < value) {
cout << prompt;
cin >> value;
cin.ignore(0x1000, '\n');
}
return value;
}
int main() {
constexpr size_t lim = 25;
array<int, lim> box{};
const auto n = length("n: ", lim);
uniform_int_distribution<> uid(10, 99);
mt19937 gen{ random_device()() };
for (size_t i = 0; i < n; ++i) {
box[i] = uid(gen);
}
if (1 == n) {
cout
<< box[0] << '\n'
<< box[0] << '\n'
<< box[0] << '\n';
} else if (2 == n) {
cout
<< box[0] << ' ' << box[1] << '\n'
<< box[0] << '\n'
<< box[0] + box[1] << '\n';
} else if (2 < n) {
const auto last = n - 1;
auto max = box[0] + box[last];
auto target = last;
for (size_t i = 1; i < n; ++i) {
const auto pos = i - 1;
const auto sum = box[i] + box[pos];
if (max < sum) {
max = sum;
target = pos;
}
}
cout << "array:";
for (size_t i = 0; i < n; ++i) {
cout << ' ' << box[i];
}
cout
<< '\n'
<< "element: " << box[target] << '\n'
<< "sum: " << max << '\n';
}
}