1683:(入门)递归法求最大值
#include <iostream>
using namespace std;
int* findMax(int arr[], int left, int right) {
if (left == right) {
int* res = new int;
res = arr[left];
res = left;
return res;
}
int mid = left + (right - left) / 2;
int* leftRes = findMax(arr, left, mid);
int* rightRes = findMax(arr, mid + 1, right);
int* res = new int;
if (leftRes > rightRes) {
res = leftRes;
res = leftRes;
} else {
res = rightRes;
res = rightRes;
}
delete[] leftRes;
delete[] rightRes;
return res;
}
int main() {
int arr[] = {1, 5, 3, 7, 2};
int* res = findMax(arr, 0, 4);
cout << "最大数是 " << res << ",在数组中的位置是 " << res << endl;
delete[] res;
return 0;
}
如果郭漫看到了这里,我想说:不是你怎么还能选择性耳聋的,还能这么玩的喽,我真的服了