@Alllll0235
2017-08-30T11:58:58.000000Z
字数 1035
阅读 906
learing
//寻找行中的最大值int findRowPeak(int row, int** A, int start, int end){int peak = 0;for (int i = start; i <= end; i++)if (A[row][i]>A[row][peak])peak = i;return peak;}//寻找列中最大值int findColPeak(int col, int** A, int start, int end){int peak = 0;for (int i = start; i <= end; i++)if (A[i][col]>A[peak][col])peak = i;return peak;}int findTwoDimPeak(int**a, int n, int m){//初始化行的二分边界,还有列的二分边界int rowmin = 0, rowmax = n - 1, rowmid = rowmax / 2;int colmin = 0, colmax = m - 1, colmid = colmax / 2;while (rowmin <= rowmax && colmin <= colmax){//计算行的二分中点rowmid =(rowmax - rowmin)/2+rowmin;//找出中间行的最大数int rowpeak = findRowPeak(rowmid, a, colmin, colmax);//比较最大数与上面和下面的数的大小if (rowmid!=rowmax&&a[rowmid][rowpeak] < a[rowmid + 1][rowpeak]){rowmin = rowmid + 1;}else if (rowmid!=rowmin&&a[rowmid][rowpeak] < a[rowmid - 1][rowpeak]){rowmax = rowmid - 1;}elsereturn a[rowmid][rowpeak];//计算列的二分中点colmid = colmin + ((colmax - colmin) / 2);int colpeak = findColPeak(colmid, a, rowmin, rowmax);if (a[colpeak][colmid] < a[colpeak][colmid + 1]){colmin = colmid + 1;}else if (a[colpeak][colmid] < a[colpeak][colmid - 1]){colmax = colmid - 1;}else return a[colpeak][colmid];}return a[rowmid][colmid];}