C/C++

Массивы в С++

. Получить: max (a1 + an, a2 + an-1, ..., an/2 + an/2+1).
#include <iostream>
#include <ctime>

using namespace std;

int main()
{
const int n = 12;
srand(time(NULL));
int a[n];
for(int x = 0; x < n; x++)
{
a[x] = rand() % (2*n);
cout << a[x] << ' ';
}
cout << endl << endl;
int max = a[0] + a[n-1];
for(int x = 1; x < n/2; x++)
if(a[x] + a[n-1-x] > max)
max = a[x] + a[n-1-x];

cout << max << endl;

return 0;
}
Георгий Любитель Оргий
Георгий Любитель Оргий
71 756
Лучший ответ
Георгий Любитель Оргий #include <iostream>
#include <ctime>

using namespace std;

int main()
{
const int n = 12;
srand(time(NULL));
int a[n];
for(int x = 0; x < n; x++)
{
a[x] = rand() % (2*n);
cout << a[x] << ' ';
}
cout << endl << endl;

int max = a[0] + a[n-1];
for(int x = 1; x < n/2; x++)
if(a[x] + a[n-1-x] > max)
max = a[x] + a[n-1-x];

cout << max << endl << endl;

int b[] = {14, 2, 3, 1, 25, 8, 6, 9, 5, 7, 10, 18};
for(int x = 0; x < n; x++) cout << b[x] << ' ';
cout << endl << endl;
max = b[0] + b[n-1];
for(int x = 1; x < n/2; x++)
if(b[x] + b[n-1-x] > max)
max = b[x] + b[n-1-x];
Георгий Любитель Оргий cout << max << endl << endl;

int c[n];
for(int x = 0; x < n; x++) cin >> c[x];
cout << endl;
max = c[0] + c[n-1];
for(int x = 1; x < n/2; x++)
if(c[x] + c[n-1-x] > max)
max = c[x] + c[n-1-x];

cout << max << endl;

return 0;
}
#include <iostream>
#include <random>
#include <set>
using namespace std;
unsigned integer(const char* msg) {
auto value = 0U;
while (!value || value & 1) {
cout << msg;
cin >> value;
cin.ignore(cin.rdbuf()->in_avail());
}
return value;
}
long long max(int* seq, const size_t len) {
set<long long, greater<>> box;
for (auto i = 0U, j = len - 1; i < j; ++i, --j) {
box.insert(long long(seq[i]) + seq[j]);
}
return *box.begin();
}
void test_1() {
int seq[] = { 23, 14, 57, 48, 69, 35, 41, 87, 62, 14, 33, 64 };
for (auto item : seq) cout << item << ' ';
puts("");
auto res = max(seq, size(seq));
cout << "max: " << res << '\n';
}
void test_2(const size_t len) {
auto seq = new int[len];
cout << "Elements: ";
for (auto i = 0U; i < len; ++i) cin >> seq[i];
auto res = max(seq, len);
cout << "max: " << res << '\n';
delete[] seq;
}
void test_3(int* seq, const size_t len) {
uniform_int_distribution<> uid(10, 99);
mt19937 gen{ random_device()() };
for (auto i = 0U; i < len; ++i) {
seq[i] = uid(gen);
cout << seq[i] << ' ';
}
puts("");
auto res = max(seq, len);
cout << "max: " << res << '\n';
}
int main() {
test_1();
auto len = integer("Length: ");
test_2(len);
auto seq = new int[len];
test_3(seq, len);
delete[] seq;
system("pause > nul");
}
AE
Abbaseli Elizade
68 957