
C/C++
Помогите решить задачу в С++

#include <iostream>
#include <iomanip>
#include <random>
#include <cstdlib>
using namespace std;
int main() {
cout << "a: ";
int a;
cin >> a;
cout << "b: ";
int b;
cin >> b;
if (a > b) exit(0);
cout << "n: ";
size_t n;
cin >> n;
puts("");
auto matrix = new(nothrow) int* [n];
if (!matrix) exit(0);
for (auto i = 0U; i < n; ++i) {
matrix[i] = new(nothrow) int[n];
if (!matrix[i]) {
for (auto j = 0U; j < i; ++j) {
delete[] matrix[j];
}
delete[] matrix;
matrix = nullptr;
break;
}
}
if (!matrix) exit(0);
uniform_int_distribution<> uid(a, b);
mt19937 gen{ random_device()() };
for (auto i = 0U; i < n; ++i) {
for (auto j = 0U; j < n; ++j) {
matrix[i][j] = uid(gen);
}
}
streamsize w = 4U;
if (a || b) {
w += static_cast<streamsize>(log10(abs(a) > abs(b) ? abs(a) : abs(b)));
}
for (auto i = 0U; i < n; ++i) {
for (auto j = 0U; j < n; ++j) {
cout << setw(w) << matrix[i][j];
}
puts("\n");
}
auto max_neg = numeric_limits<int>::min();
auto min_pos = numeric_limits<int>::max();
for (auto i = 0U; i < n; ++i) {
for (auto j = 0U; j < n; ++j) {
if (i < (n >> 1)) {
if (i >= j && matrix[i][j] < 0 && matrix[i][j] > max_neg) {
max_neg = matrix[i][j];
}
if (i + j >= n - 1 && matrix[i][j] > 0 && matrix[i][j] < min_pos) {
min_pos = matrix[i][j];
}
} else {
if (i <= j && matrix[i][j] > 0 && matrix[i][j] < min_pos) {
min_pos = matrix[i][j];
}
if (i + j < n && matrix[i][j] < 0 && matrix[i][j] > max_neg) {
max_neg = matrix[i][j];
}
}
}
}
if (max_neg != numeric_limits<int>::min()) {
cout << "Max negative: " << max_neg << '\n';
} else {
puts("Max negative not found!");
}
if (min_pos != numeric_limits<int>::max()) {
cout << "Min positive: " << min_pos << '\n';
} else {
puts("Min positive not found!");
}
for (auto i = 0U; i < n; ++i) {
delete[] matrix[i];
}
delete[] matrix;
system("pause > nul");
}
#include <iomanip>
#include <random>
#include <cstdlib>
using namespace std;
int main() {
cout << "a: ";
int a;
cin >> a;
cout << "b: ";
int b;
cin >> b;
if (a > b) exit(0);
cout << "n: ";
size_t n;
cin >> n;
puts("");
auto matrix = new(nothrow) int* [n];
if (!matrix) exit(0);
for (auto i = 0U; i < n; ++i) {
matrix[i] = new(nothrow) int[n];
if (!matrix[i]) {
for (auto j = 0U; j < i; ++j) {
delete[] matrix[j];
}
delete[] matrix;
matrix = nullptr;
break;
}
}
if (!matrix) exit(0);
uniform_int_distribution<> uid(a, b);
mt19937 gen{ random_device()() };
for (auto i = 0U; i < n; ++i) {
for (auto j = 0U; j < n; ++j) {
matrix[i][j] = uid(gen);
}
}
streamsize w = 4U;
if (a || b) {
w += static_cast<streamsize>(log10(abs(a) > abs(b) ? abs(a) : abs(b)));
}
for (auto i = 0U; i < n; ++i) {
for (auto j = 0U; j < n; ++j) {
cout << setw(w) << matrix[i][j];
}
puts("\n");
}
auto max_neg = numeric_limits<int>::min();
auto min_pos = numeric_limits<int>::max();
for (auto i = 0U; i < n; ++i) {
for (auto j = 0U; j < n; ++j) {
if (i < (n >> 1)) {
if (i >= j && matrix[i][j] < 0 && matrix[i][j] > max_neg) {
max_neg = matrix[i][j];
}
if (i + j >= n - 1 && matrix[i][j] > 0 && matrix[i][j] < min_pos) {
min_pos = matrix[i][j];
}
} else {
if (i <= j && matrix[i][j] > 0 && matrix[i][j] < min_pos) {
min_pos = matrix[i][j];
}
if (i + j < n && matrix[i][j] < 0 && matrix[i][j] > max_neg) {
max_neg = matrix[i][j];
}
}
}
}
if (max_neg != numeric_limits<int>::min()) {
cout << "Max negative: " << max_neg << '\n';
} else {
puts("Max negative not found!");
}
if (min_pos != numeric_limits<int>::max()) {
cout << "Min positive: " << min_pos << '\n';
} else {
puts("Min positive not found!");
}
for (auto i = 0U; i < n; ++i) {
delete[] matrix[i];
}
delete[] matrix;
system("pause > nul");
}
Похожие вопросы
- Помогите решить задачу по программированию на C++
- Помогите решить задачу по C++!
- Помогите решить задачу по программированию
- Помогите решить задачу пожалуйста, в C++
- Помогите решить задачу на c++
- Помогите решить задачу на С++ (мне не совсем ясен смысл задания)
- Помогите решить задачу на С++, используя статические массивы
- Товарищи программисты,помогите решить задачу для 1 курса .
- Помогите решить задачу на С++
- Помогите решить задачу на C++