剑指offer:3-二维数组中的查找

来源:互联网 发布:淘宝网1 编辑:程序博客网 时间:2024/05/18 12:41

原文地址:http://blog.csdn.net/xiaofei2010/article/details/8898116


代码如下:

#include <iostream>using namespace std;/*//版本1:从二维数组右上角开始比较//从一个二维数组matrix中查找数字number,数组总行数为rows,总列数为columns.bool find_number_from_array(int *matrix,int rows,int columns,int number){bool found = false;if (matrix != NULL && rows > 0 && columns > 0){//初始化行列,取右上角位置数字.int row = 0;int column = columns - 1;while (row < rows && column >= 0){//number刚好等于二维矩阵右上角的数字.if (number == matrix[row * columns + column]){found = true;break;}//number大于二维矩阵右上角的数字,删除该数字所在的行.else if(number > matrix[row * columns + column])++row;//number小于二维矩阵右上角的数字,删除该数字所在的列.else--column;}}//返回值found为true表示找到number,为false表示未找到number.return found;}*///版本2:从二维数组左下角开始比较//从一个二维数组matrix中查找数字number,数组总行数为rows,总列数为columns.bool find_number_from_array(int *matrix,int rows,int columns,int number){bool found = false;if (matrix != NULL && rows > 0 && columns > 0){//初始化行列,取左下角位置数字.int row = rows - 1;int column = 0;while (row >= 0 && column < columns){//number刚好等于二维矩阵左下角的数字.if (number == matrix[row * columns + column]){found = true;break;}//number大于二维矩阵右上角的数字,删除该数字所在的行.else if(number > matrix[row * columns + column])++column;//number小于二维矩阵右上角的数字,删除该数字所在的列.else--row;}}//返回值found为true表示找到number,为false表示未找到number.return found;}int main(){int a[] = {1,2,8,9,2,4,9,12,4,7,10,13,6,8,11,15};int num;cout << "输入想要查找的数字:" << endl;while(cin >> num){if(find_number_from_array(a,4,4,num))cout << "找到!" << endl;elsecout << "未找到!" << endl;}return 0;}

测试代码

// ====================测试代码====================void Test(char* testName, int* matrix, int rows, int columns, int number, bool expected){if(testName != NULL)printf("%s begins: ", testName);bool result = Find(matrix, rows, columns, number);if(result == expected)printf("Passed.\n");elseprintf("Failed.\n");}//  1   2   8   9//  2   4   9   12//  4   7   10  13//  6   8   11  15// 要查找的数在数组中void Test1(){int matrix[][4] = {{1, 2, 8, 9}, {2, 4, 9, 12}, {4, 7, 10, 13}, {6, 8, 11, 15}};Test("Test1", (int*)matrix, 4, 4, 7, true);}//  1   2   8   9//  2   4   9   12//  4   7   10  13//  6   8   11  15// 要查找的数不在数组中void Test2(){int matrix[][4] = {{1, 2, 8, 9}, {2, 4, 9, 12}, {4, 7, 10, 13}, {6, 8, 11, 15}};Test("Test2", (int*)matrix, 4, 4, 5, false);}//  1   2   8   9//  2   4   9   12//  4   7   10  13//  6   8   11  15// 要查找的数是数组中最小的数字void Test3(){int matrix[][4] = {{1, 2, 8, 9}, {2, 4, 9, 12}, {4, 7, 10, 13}, {6, 8, 11, 15}};Test("Test3", (int*)matrix, 4, 4, 1, true);}//  1   2   8   9//  2   4   9   12//  4   7   10  13//  6   8   11  15// 要查找的数是数组中最大的数字void Test4(){int matrix[][4] = {{1, 2, 8, 9}, {2, 4, 9, 12}, {4, 7, 10, 13}, {6, 8, 11, 15}};Test("Test4", (int*)matrix, 4, 4, 15, true);}//  1   2   8   9//  2   4   9   12//  4   7   10  13//  6   8   11  15// 要查找的数比数组中最小的数字还小void Test5(){int matrix[][4] = {{1, 2, 8, 9}, {2, 4, 9, 12}, {4, 7, 10, 13}, {6, 8, 11, 15}};Test("Test5", (int*)matrix, 4, 4, 0, false);}//  1   2   8   9//  2   4   9   12//  4   7   10  13//  6   8   11  15// 要查找的数比数组中最大的数字还大void Test6(){int matrix[][4] = {{1, 2, 8, 9}, {2, 4, 9, 12}, {4, 7, 10, 13}, {6, 8, 11, 15}};Test("Test6", (int*)matrix, 4, 4, 16, false);}// 鲁棒性测试,输入空指针void Test7(){Test("Test7", NULL, 0, 0, 16, false);}int main(){Test1();Test2();Test3();Test4();Test5();Test6();Test7();return 0;}



1 0