zoukankan      html  css  js  c++  java
  • 二维数组的查找

    题目:在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。


           当我们需要解决一个复杂的问题时,一个很有效的办法就是从一个具体的问题入手,通过分析简单具体的例子,试图寻找普遍的规律。针对这个问题,我们不妨也从一个具体的例子入手。下面我们以在题目中给出的数组中查找数字7为例来一步步分析查找的过程。

           我们发现如下规律:首先选取数组中右上角的数字。如果该数字等于要查找的数字,查找过程结束;如果该数字大于要查找的数字,剔除这个数字所在的列;如果该 数字小于要查找的数字,剔除这个数字所在的行。也就是说如果要查找的数字不在数组的右上角,则每一次都在数组的查找范围中剔除一行或者一列,这样每一步都 可以缩小查找的范围,直到找到要查找的数字,或者查找范围为空。

         

          把整个查找过程分析清楚之后,我们再写代码就不是一件很难的事情了。下面是上述思路对应的参考代码:

    1. bool Find(int* matrix, int rows, int columns, int number)  
    2. {  
    3.     bool found = false;  
    4.   
    5.     if(matrix != NULL && rows > 0 && columns > 0)  
    6.     {  
    7.         int row = 0;  
    8.         int column = columns - 1;  
    9.         while(row < rows && column >=0)  
    10.         {  
    11.             if(matrix[row * columns + column] == number)  
    12.             {  
    13.                 found = true;  
    14.                 break;  
    15.             }  
    16.             else if(matrix[row * columns + column] > number)  
    17.                 -- column;  
    18.             else  
    19.                 ++ row;  
    20.         }  
    21.     }  
    22.   
    23.     return found;  
    24. }  

           在前面的分析中,我们每一次都是选取数组查找范围内的右上角数字。同样,我们也可以选取左下角的数字。感兴趣的读者不妨自己分析一下每次都选取左下角的查 找过程。但我们不能选择左上角或者右下角。以左上角为例,最初数字1位于初始数组的左上角,由于1小于7,那么7应该位于1的右边或者下边。此时我们既不 能从查找范围内剔除1所在的行,也不能剔除1所在的列,这样我们就无法缩小查找的范围。

    二维数组的乘法实现可参考:http://www.cnblogs.com/heyonggang/p/3262069.html

    实现代码如下:

     1 #include<iostream>
     2 using namespace std;
     3 
     4 // 二维数组matrix中,每一行都从左到右递增排序,
     5 // 每一列都从上到下递增排序
     6 bool Find(int* matrix, int rows, int columns, int number)
     7 {
     8     bool found = false;
     9 
    10     if(matrix != NULL && rows > 0 && columns > 0)
    11     {
    12         int row = 0;
    13         int column = columns - 1;
    14         while(row < rows && column >=0)
    15         {
    16             if(matrix[row * columns + column] == number)
    17             {
    18                 found = true;
    19                 break;
    20             }
    21             else if(matrix[row * columns + column] > number)
    22                 -- column;
    23             else
    24                 ++ row;
    25         }
    26     }
    27 
    28     return found;
    29 }
    30 
    31 int main()
    32 {
    33     int matrix[][4] = {{1, 2, 8, 9}, {2, 4, 9, 12}, {4, 7, 10, 13}, {6, 8, 11, 15}};
    34     bool result = Find((int*)matrix , 4 , 4 , 7); //这里实参是(int*)matrix
    35     if(result == true)
    36         printf("Passed.
    ");
    37     else
    38         printf("Failed.
    ");
    39     return 0;
    40 
    41 }

    测试代码如下:

      1. #include<iostream>  
      2. using namespace std;  
      3.   
      4.   
      5. // 二维数组matrix中,每一行都从左到右递增排序,  
      6. // 每一列都从上到下递增排序  
      7. bool Find(int* matrix, int rows, int columns, int number)  
      8. {  
      9.     bool found = false;  
      10.   
      11.     if(matrix != NULL && rows > 0 && columns > 0)  
      12.     {  
      13.         int row = 0;  
      14.         int column = columns - 1;  
      15.         while(row < rows && column >=0)  
      16.         {  
      17.             if(matrix[row * columns + column] == number)  
      18.             {  
      19.                 found = true;  
      20.                 break;  
      21.             }  
      22.             else if(matrix[row * columns + column] > number)  
      23.                 -- column;  
      24.             else  
      25.                 ++ row;  
      26.         }  
      27.     }  
      28.   
      29.     return found;  
      30. }  
      31.   
      32.   
      33. void Test(char* testName, int* matrix, int rows, int columns, int number)  
      34. {  
      35.     if(testName != NULL)  
      36.         printf("%s begins: ", testName);  
      37.   
      38.     bool result = Find(matrix, rows, columns, number);  
      39.     if(result == true)  
      40.         printf("Passed. ");  
      41.     else  
      42.         printf("Failed. ");  
      43. }  
      44.   
      45. // 要查找的数在数组中  
      46. void Test1()  
      47. {  
      48.     int matrix[][4] = {{1, 2, 8, 9}, {2, 4, 9, 12}, {4, 7, 10, 13}, {6, 8, 11, 15}};  
      49.     Test("Test1", (int*)matrix, 4, 4, 7);  
      50. }  
      51.   
      52. // 要查找的数不在数组中  
      53. void Test2()  
      54. {  
      55.     int matrix[][4] = {{1, 2, 8, 9}, {2, 4, 9, 12}, {4, 7, 10, 13}, {6, 8, 11, 15}};  
      56.     Test("Test2", (int*)matrix, 4, 4, 5);  
      57. }  
      58.   
      59.   
      60.   
      61. int main(int argc, char * argv[])  
      62. {  
      63.     Test1();  
      64.     Test2();  
      65.    
      66.     return 0;  
      67. }  
  • 相关阅读:
    PAT (Advanced Level) Practice 1071 Speech Patterns (25分)
    PAT (Advanced Level) Practice 1070 Mooncake (25分)
    PAT (Advanced Level) Practice 1069 The Black Hole of Numbers (20分)
    PAT (Advanced Level) Practice 1074 Reversing Linked List (25分)
    PAT (Advanced Level) Practice 1073 Scientific Notation (20分)
    第一次冲刺个人总结01
    构建之法阅读笔记01
    人月神话阅读笔记01
    四则运算2
    学习进度条(软件工程概论1-8周)
  • 原文地址:https://www.cnblogs.com/heyonggang/p/3262076.html
Copyright © 2011-2022 走看看