Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <tuple>
- using namespace std;
- void input(int* arr, int n, bool rm) {
- for (int i = 0; i < n; ++i) {
- if (rm && i + 1 == n) {
- arr[i] = arr[i - 1];
- break;
- }
- cout << "Podaj " << i + 1 << " liczbę: ";
- cin >> arr[i];
- }
- }
- void out(int* arr, int n) {
- cout << "[";
- for (int i = 0; i < n - 1; ++i)
- cout << arr[i] << ", ";
- cout << arr[n - 1] << "]" << endl;
- }
- int ext(int* arr, int n, bool mini = true) {
- int m = arr[0];
- for (int i = 0; i < n; ++i) {
- if (mini) {
- if (arr[i] < m) m = arr[i];
- } else {
- if (arr[i] > m) m = arr[i];
- }
- }
- return m;
- }
- tuple<int, int> conquer(int* arr, int n) {
- int* low = new int[n / 2];
- int* high = new int[n / 2];
- for (int i = 0; i < n / 2; i++) {
- if (arr[i * 2] < arr[i * 2 + 1]) {
- low[i] = arr[i * 2];
- high[i] = arr[i * 2 + 1];
- }
- else {
- high[i] = arr[i * 2];
- low[i] = arr[i * 2 + 1];
- }
- }
- cout << "low: "; out(low, n / 2);
- cout << "high: "; out(high, n /2);
- return { ext(low, n / 2), ext(high, n / 2, false) };
- delete[] low;
- delete[] high;
- }
- int main() {
- int n, min, max;
- bool rm = false;
- cout << "Ile liczb: ";
- cin >> n;
- if (n % 2 == 1) { n++; rm = true; }
- int *arr = new int[n];
- input(arr, n, rm);
- tie(min, max) = conquer(arr, n);
- cout << "min: " << min << endl << "max: " << max << endl << "rozpiętość: " << max - min << endl;
- delete[] arr;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement