Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdlib> // Для работы qsort
- using namespace std;
- int **createMatrix(int a, int b) // Функция создания многомерного массива с заданным кол-вом строк и столбцов
- {
- int **matrix = new int *[a];
- for (int i = 0; i < a; i++)
- matrix[i] = new int[b];
- return matrix;
- }
- int compare(const void * x1, const void * x2) // Функция сравнения элементов массива
- {
- return ( *(int*)x1 - *(int*)x2 ); // Если результат вычитания равен 0, то числа равны, < 0: x1 < x2; > 0: x1 > x2
- }
- int main()
- {
- int n;
- int maxsum = 0;
- int minsum = 0;
- cout << "Enter the number of pairs: ";
- cin >> n;
- /*int *A = new int [n]; // Способ через 2 одномерных массива
- int *B = new int [n];
- for (int i = 0; i < n; i++)
- {
- cout << "A[" << i + 1 << "]: ";
- cin >> A[i];
- }
- for (int i = 0; i < n; i++)
- {
- cout << "B[" << i + 1 << "]: ";
- cin >> B[i];
- }
- for (int i = 0; i < n; i++) // Вывод неотсортированного массива 1
- {
- cout << A[i] << ' ';
- }
- for (int i = 0; i < n; i++) // Вывод неотсортированного массива 2
- {
- cout << B[i] << ' ';
- }
- qsort(A, n, sizeof(int), compare); // Сортировка
- qsort(B, n, sizeof(int), compare);
- cout << "\n" << "___________________________" << "\n";
- for (int i = 0; i < n; i++) // Вывод отсортированного массива 1
- {
- cout << A[i] << ' ';
- }
- cout << "\n";
- for (int i = 0; i < n; i++) // Вывод отсортированного массива 2
- {
- cout << B[i] << ' ';
- }
- for (int j = 0; j < n; j++) // Условие расчёта максимальной суммы
- maxsum += A[j] * B[j];
- cout << "\n" << "Max sum: " << maxsum << "\n";
- for (int j = n-1; j >= (n-1)/2; j--) // Условие расчёта минимальной суммы
- {
- if (j != n-1-j)
- minsum += (A[j] * B[n-1-j]) + (A[n-1-j] * B[j]);
- else minsum += (A[j] * B[n-1-j]);
- if (j == n-j) break;
- }
- cout << "Min sum: " << minsum << "\n" ;
- */
- int **matrix = createMatrix(2,n); // Способ через один двумерный массив
- for (int i = 0; i < 2; i++)
- for (int j = 0; j < n; j++)
- {
- cout << "A[" << i + 1 << "][" << j+1 << "] : ";
- cin >> matrix[i][j];
- }
- for (int i = 0; i < 2; i++) // Вывод неотсортированной матрицы
- {
- cout << "\n";
- for (int j = 0; j < n; j++)
- cout << matrix[i][j] << ' ';
- }
- for (int i = 0; i < 2; i++) // Сортировка
- qsort(matrix[i], n, sizeof(int), compare);
- cout << "\n" << "___________________________" << "\n";
- for (int i = 0; i < 2; i++) // Вывод отсортированной матрицы
- {
- cout << "\n";
- for (int j = 0; j < n; j++)
- cout << matrix[i][j] << ' ';
- }
- for (int j = 0; j < n; j++) // Условие расчёта максимальной суммы
- maxsum += matrix[0][j] * matrix[1][j];
- cout << "\n" << "Max sum: " << maxsum << "\n";
- for (int j = n-1; j >= (n-1)/2; j--) // Условие расчёта минимальной суммы
- {
- if (j != n-1-j)
- minsum += (matrix[0][j] * matrix[1][n-1-j]) + (matrix[0][n-1-j] * matrix[1][j]);
- else minsum += (matrix[0][j] * matrix[1][n-1-j]);
- if (j == n-j) break;
- }
- cout << "Min sum: " << minsum << "\n" ;
- system("PAUSE");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement