Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdlib>
- #include <ctime>
- #include <iostream>
- using namespace std;
- template <class T>
- struct MaxMin {
- T *max, *min;
- };
- template <class T>
- MaxMin<T> findMaxMin(T* lh, T* rh)
- {
- if (lh == rh) {
- return { lh, rh };
- }
- T* mid = lh + (rh - lh) / 2;
- MaxMin<T> mml = findMaxMin(lh, mid);
- MaxMin<T> mmr = findMaxMin(mid + 1, rh);
- return {
- (*(mml.max) < *(mmr.max) ? mmr.max : mml.max),
- (*(mml.min) > *(mmr.min) ? mmr.min : mml.min)
- };
- }
- int main()
- {
- srand(time(NULL));
- int arr[10] = { 0 };
- for (int i = 0; i < 10; i++) {
- arr[i] = rand() % 100;
- cout << arr[i] << " ";
- }
- cout << endl;
- MaxMin<int> res = findMaxMin(arr, arr + 9);
- cout << "max: " << *(res.max) << endl;
- cout << "min: " << *(res.min) << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement