To prove safety notes offer face questions 4 ---- two-dimensional array lookup

Title: In a two-dimensional array, each row from left to right in order of ascending sort each column are sorted in ascending order from top to bottom. A complete function, enter such a two-dimensional array and an integer, it is determined whether the array contains the integer.

Test Case:

  • Find dimensional array contains a number of (an array lookup number is maximum and a minimum; array between maximum and minimum values ​​between digital lookup).
  • You do not find a digital two-dimensional array (digital lookup greater than the maximum array; is less than the minimum digital lookup array; digital lookup array, but this number is not between the maximum and minimum values ​​of the array).
  • Special test input (input null pointer).

Test code:

/*
***数字包含在数组中(为数组中的最大值或最小值或介于最大与最小之间)
***数字不包含在数组中(大于最大值或小于最小值或介于最大与最小之间但不包含)
***输入数组为空指针 
*/
void test(char* testName, int* matrix, int rows, int columns, int number, bool expected){
    if(testName != nullptr){
        printf("%s begins: ", testName);
    }
    bool result = find(matrix, rows, columns, number);
    if(result == expected){
        printf("Passed.\n");
    }
    else{
        printf("Failed.\n");
    }
} 

/*******数组********/ 
// 1 2 8 9
// 2 4 9 12
// 4 7 10 13
// 6 8 11 15
int matrix[][4] = {{1, 2, 8, 9}, {2, 4, 9, 12}, {4, 7, 10, 13}, {6, 8, 11, 15}};

//要查找的数在数组中
void test1(){
    test("test1", (int*)matrix, 4, 4, 7, true);
} 

//要查找的数不在数组中
void test2(){
    test("test2", (int*)matrix, 4, 4, 5, false);
} 

//要查找的数是数组中最小的数字
void test3(){
    test("test3", (int*)matrix, 4, 4, 1, true);
} 

//要查找的数是数组中的最大的数字
void test4(){
    test("test4", (int*)matrix, 4, 4, 15, true);
} 

//要查找的数比数组中最小的数字还小
void test5(){
    test("test5", (int*)matrix, 4, 4, 0, false);
} 

//要查找的数比数组中最大的数字还大
void test6(){
    test("test6", (int*)matrix, 4, 4, 16, false);
} 

//鲁棒性测试,输入空指针
void test7(){
    test("test7", nullptr, 0, 0, 16, false);
} 

This question test sites:

  • Examine the candidate's ability to understand and program for two-dimensional array. Two-dimensional array occupy contiguous space in memory. Stored in memory elements in the rows from top to bottom, left to right in the order stored in the same row. Therefore, we can calculate the row and column numbers in accordance with the relative offset of the first address of the array in order to find the corresponding element.
  • Examine the candidates ability to analyze problems. When the candidates found the problem more complicated, can not find out the rules by specific examples of them, it is the key to whether the solution to this problem. As long as the subject from the upper right corner of a specific two-dimensional array of start analysis, you can find the law to find to find a breakthrough to solve the problem.

Implementation code:

#include <cstdio>

bool find(int* matrix, int rows, int columns, int number){
    bool found = false;
    if(matrix != nullptr && rows > 0 && columns > 0){
        int row = 0;
        int column = columns - 1;
        while(row < rows && column >= 0){
            if(matrix[row*columns+column] == number){
                found = true;
                break;
            }
            else if(matrix[row*columns+column] > number){
                column--;
            }
            else{
                row++;
            }
        }
    }
    return found;
}
int main(){
    test1();
    test2();
    test3();
    test4();
    test5();
    test6();
    test7();
    return 0;
}

Guess you like

Origin www.cnblogs.com/tangliang39/p/11694332.html