编辑代码

#define _CRT_NO_SECURE_WARNINGS
 
#include<stdio.h>
#include<iostream>
 
using namespace std;
 
int str[2] = { 0 };
 
//i是数组首位标记,j是数组的末尾标记
int* MAXMIN(int i, int j, int* a) {
	int max = 0;
	int min = 0;
	int mid = 0;
	int lmax = 0;
	int lmin = 0;
	int rmax = 0;
	int rmin = 0;
	int* ltem;
	int* rtem;
	//i,j指向同一个位置,相当于一个元素
	if (i == j) {
		max = a[i];
		str[0] = max;
		min = a[i];
		str[1] = min;
	}
	//i和j差一个位置,相当于两个元素
	else if (i == j - 1) {
		//a[i]元素小
		if (a[i] < a[j]) {
			max = a[j];
			str[0] = max;
			min = a[i];
			str[1] = min;
		}
		//a[j]元素小
		else {
			max = a[i];
			str[0] = max;
			min = a[j];
			str[1] = min;
		}
	}
	//i和j指向一个数组,分治
	else {
		mid = (i + j) / 2;
		//对于i到mid的数求最大最小值
		ltem = MAXMIN(i, mid, a);
		lmax = *ltem;
		lmin = *(ltem + 1);
		//对于mid+1到j的数求最大最小值
		rtem = MAXMIN(mid + 1, j, a);
		rmax = *rtem;
		rmin = *(rtem + 1);
		//对比左右数列的最大最小值
		if (lmax > rmax) {
			max = lmax;
			str[0] = max;
		}
		else {
			max = rmax;
			str[0] = max;
		}
		if (lmin > rmin) {
			min = rmin;
			str[1] = min;
		}
		else {
			min = lmin;
			str[1] = min;
		}
	}
	return str;
}
 
int main() {
	int a[100] = { 0 };
	int j = 0;
	int k = 0;
	int* p;
	char c ='0';
		
	cout << "请输入要求最大最小值的数列:"<<endl;
	for (j = 0;j < 100;j++) {
		cin >> a[j];
		if (getchar() == '\n') {
			break;
		}
	}
 
	p=MAXMIN(0, j, a);
	cout << "此序列最大值为:" << *p << endl;
	cout << "此序列最小值为:" << *(p + 1) << endl;
	system("pause");
	return 0;
}