This topic to kneel, C + + death timeout ... Later, the main function is changed to use C ...
Algorithm:
/* Topic Description: In a two-dimensional array, each row is sorted in order from left to right, and each column is sorted in ascending order from top to bottom.
Please complete a function, enter such a two-dimensional array and an integer to determine whether the array contains the integer.
Input: Input may contain multiple test samples, for each test case, enter the first behavior two integers m and n (1<=m,n<=1000): Represents the number of rows and columns of the matrix that will be entered.
The second line you enter includes an integer t (1<=t<=1000000) that represents the number you want to find. Back to the Column page: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/the next M row, each row has n number, representing the problem given by the M row n column matrix (matrix as shown in the topic description,
Each row is sorted in ascending order from left to right, and each column is sorted in ascending order from top to bottom.
Output: For each test case, the output "Yes" represents the number T found in the two-dimensional array.
The output "No" means that no number T is found in the two-dimensional array. Example input: 3 3 5 1 2 3 4 5 6 7 8 9 3 3 1 2 3 4 5 6 7 8 9 10 3 3 12 2 3 4 5 6 7 8 9 The sample output: Yes No no----- --------------------------------------------principle: Each time you select a number in the upper-right corner of the array to compare, if the number is larger than the number you are looking for, delete the column; If the number is smaller than the number you are looking for, remove the row;
Each time you can reduce the two-dimensional array by one row or column, you can finally find the element.
* * #include <iostream> #include <stdlib.h> #include <stdio.h> using namespace std;
#define MAX 1000000 bool Find (int arr[],int row,int column,int target) {int r,c;
if (arr==null | | row<1 | | column<1) {return false; } r = 0,c = column-1; while (r<=row-1 && c>=0) {if (target = = Arr[r*column+c]) {return Tru
E
}else if (Target > Arr[r*column+c]) {r++;
}else {c--;
return false;
int main () {int m,n;
while (scanf ("%d%d", &m,&n)!= EOF) {int key,i;
static int Matrix[max] = {0};
scanf ("%d", &key);
for (i=0;i<m*n;i++) scanf ("%d", matrix+i);
BOOL result = find (Matrix,m,n,key);
if (result) printf ("yes\n");
else printf ("no\n");
return 0; }
Author: csdn Blog ROWANDJJ