1 #include<iostream>
2 using namespace std;
3 #define MAX 255
4 /*
5 *题目:在一个二维数组中,每一行都按照从左到右递增的顺序排序,
6 *每一列都按照从上到下递增的顺序排列。输入一个这样的二维数组和一个整数
7 *判断数组中是否有该整数。
8 方法:首先选取数组中右上角的数字。如果该数字等于要查找的数字,查找过程结束,
9 如果该数字大于要查找的数字,剔除这个数字所在的列;如果该数字小于要查找的数字
10 ,剔除这个数字所在的行。
11 **/
12 bool Find(int* matrix, int rows, int columns, int number) {
13 bool found = false;
14 if(matrix != NULL && rows > 0 && columns >0) {
15 int row = 0;
16 int column = columns - 1;
17 while(row < rows && column >= 0) {
18 if(matrix[row * columns + column] == number) {
19 found = true;
20 break;
21 }
22 else if(matrix[row * columns + column] > number) {
23 --column;
24 } else ++row;
25 }
26 }
27 return found;
28 }
29
30 int main()
31 {
32 int matrix[4][4] = {{1,2,8,9},{2,4,9,12},{4,7,10,13},{6,8,11,15}};
33 cout << boolalpha << Find(matrix[0], 4, 4, 14) << endl;
34 return 0;
35 }