剑指offer——面试题4:二维数组中的查找

  1 // 面试题4:二维数组中的查找
  2 // 题目:在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按
  3 // 照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个
  4 // 整数,判断数组中是否含有该整数。
  5 
  6 #include <cstdio>
  7 
  8 bool Find(int* matrix, int rows, int columns, int number)
  9 {
 10     bool found = false;
 11 
 12     if(matrix != nullptr && rows > 0 && columns > 0)
 13     {
 14         int row = 0;
 15         int column = columns - 1;
 16         while(row < rows && column >=0)
 17         {
 18             if(matrix[row * columns + column] == number)
 19             {
 20                 found = true;
 21                 break;
 22             }
 23             else if(matrix[row * columns + column] > number)
 24                 -- column;
 25             else
 26                 ++ row;
 27         }
 28     }
 29 
 30     return found;
 31 }
 32 
 33 // ====================测试代码====================
 34 void Test(char* testName, int* matrix, int rows, int columns, int number, bool expected)
 35 {
 36     if(testName != nullptr)
 37         printf("%s begins: ", testName);
 38 
 39     bool result = Find(matrix, rows, columns, number);
 40     if(result == expected)
 41         printf("Passed.\n");
 42     else
 43         printf("Failed.\n");
 44 }
 45 
 46 //  1   2   8   9
 47 //  2   4   9   12
 48 //  4   7   10  13
 49 //  6   8   11  15
 50 // 要查找的数在数组中
 51 void Test1()
 52 {
 53     int matrix[][4] = {{1, 2, 8, 9}, {2, 4, 9, 12}, {4, 7, 10, 13}, {6, 8, 11, 15}};
 54     Test("Test1", (int*)matrix, 4, 4, 7, true);
 55 }
 56 
 57 //  1   2   8   9
 58 //  2   4   9   12
 59 //  4   7   10  13
 60 //  6   8   11  15
 61 // 要查找的数不在数组中
 62 void Test2()
 63 {
 64     int matrix[][4] = {{1, 2, 8, 9}, {2, 4, 9, 12}, {4, 7, 10, 13}, {6, 8, 11, 15}};
 65     Test("Test2", (int*)matrix, 4, 4, 5, false);
 66 }
 67 
 68 //  1   2   8   9
 69 //  2   4   9   12
 70 //  4   7   10  13
 71 //  6   8   11  15
 72 // 要查找的数是数组中最小的数字
 73 void Test3()
 74 {
 75     int matrix[][4] = {{1, 2, 8, 9}, {2, 4, 9, 12}, {4, 7, 10, 13}, {6, 8, 11, 15}};
 76     Test("Test3", (int*)matrix, 4, 4, 1, true);
 77 }
 78 
 79 //  1   2   8   9
 80 //  2   4   9   12
 81 //  4   7   10  13
 82 //  6   8   11  15
 83 // 要查找的数是数组中最大的数字
 84 void Test4()
 85 {
 86     int matrix[][4] = {{1, 2, 8, 9}, {2, 4, 9, 12}, {4, 7, 10, 13}, {6, 8, 11, 15}};
 87     Test("Test4", (int*)matrix, 4, 4, 15, true);
 88 }
 89 
 90 //  1   2   8   9
 91 //  2   4   9   12
 92 //  4   7   10  13
 93 //  6   8   11  15
 94 // 要查找的数比数组中最小的数字还小
 95 void Test5()
 96 {
 97     int matrix[][4] = {{1, 2, 8, 9}, {2, 4, 9, 12}, {4, 7, 10, 13}, {6, 8, 11, 15}};
 98     Test("Test5", (int*)matrix, 4, 4, 0, false);
 99 }
100 
101 //  1   2   8   9
102 //  2   4   9   12
103 //  4   7   10  13
104 //  6   8   11  15
105 // 要查找的数比数组中最大的数字还大
106 void Test6()
107 {
108     int matrix[][4] = {{1, 2, 8, 9}, {2, 4, 9, 12}, {4, 7, 10, 13}, {6, 8, 11, 15}};
109     Test("Test6", (int*)matrix, 4, 4, 16, false);
110 }
111 
112 // 鲁棒性测试,输入空指针
113 void Test7()
114 {
115     Test("Test7", nullptr, 0, 0, 16, false);
116 }
117 
118 int main(int argc, char* argv[])
119 {
120     Test1();
121     Test2();
122     Test3();
123     Test4();
124     Test5();
125     Test6();
126     Test7();
127 
128     return 0;
129 }
View Code

猜你喜欢

转载自www.cnblogs.com/acm-jing/p/10381558.html