Now give an n element of the book group, the number of elements N. The maximum minimum value must be requested.
Method 1.
With Max,min. Records the maximum and minimum values of the array, sequentially scans the array, and constantly replaces the update max. Min, (the initial value of Max,min is the first element in the array)
Method 2.
1. Assume that there is only one element in the array. Then it's the biggest and the smallest.
2. Otherwise, more than one number in the array. It is possible to find the maximum minimum value on the left and the maximum minimum value on the right. Then the maximum value for this interval is Max (Lmax,rmax), Min is min (lmin,rmin)
Details such as the following (n numbers are randomly generated).
#include <stdio.h> #include <time.h>int getmax (int a,int b) {return a>b?a:b;} int getmin (int a,int b) {return a<b?A:B;} void print (int a[],int n) {int i; for (i=0;i<20;i++) printf ("%d", a[i]); printf ("\ n");} void maxmin (int a[],int l,int r,int * _max,int * _min) {if (r>l) {int m= (L+R)/2; int _lmin,_rmin,_lmax,_rmax; Maxmin (a,l,m,&_lmax,&_lmin); Maxmin (a,m+1,r,&_rmax,&_rmin); *_max=getmax (_lmax,_rmax); *_min=getmin (_lmin,_rmin); }else{*_max=*_min=a[l]; }}int creat (int a[],int n,int m) {int i; Srand (Time (NULL)); for (i=0;i<n;i++) A[i]=rand ()%M-M/2; return n; }int Main () {int n,i; int a[100]; int _min,_max; Creat (a,20,100); Print (a,20); Maxmin (a,0,19,&_max,&_min); printf ("max:%d min:%d\n", _max,_min); return 0;}
The following are the results of several executions.
watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvu2fwcghpcmvtdgfyda==/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma== /dissolve/70/gravity/southeast ">
To find the maximum value of the array by the method of control