zoukankan      html  css  js  c++  java
  • 《剑指offer》第四题(二维数组中的查找)

    // 二维数组中的查找
    // 题目:在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按
    // 照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个
    // 整数,判断数组中是否含有该整数。
    
    #include <iostream>
    using namespace std;
    
    bool serach_in(int* matrix, int rows, int cols, int number)
    {
        bool flag = false;
    
        if ((matrix != NULL) && (rows > 0) && (cols > 0))//判断不能忘
        {
            int row = 0, col = cols - 1;
            while ((row < rows) && (col >= 0))
            {
                if (matrix[row*cols + col] == number)
                {
                    flag = true;
                    break;
                }
                else if (matrix[row*cols + col] > number)//用实例分析过程,思想比程序更重要
                    col--;
                else
                    row++;
            }
        }
    
        return flag;
    }
    
    void test1()//要查找的数在数组中
    {
        cout << "Test1:
    ";
        int matrix[][4] = { {1, 2, 8, 9}, {2, 4, 9, 12}, {4, 7, 10, 13}, {6, 8, 11, 15} };
        bool result;
        result = serach_in(matrix[0], 4, 4, 7);
        if (result)
            cout << "Yes!
    ";
        else
            cout << "No!
    ";
    }
    
    void test2()//要查找的数不在数组中
    {
        cout << "Test2:
    ";
        int matrix[][4] = { {1, 2, 8, 9}, {2, 4, 9, 12}, {4, 7, 10, 13}, {6, 8, 11, 15} };
        bool result;
        result = serach_in(matrix[0], 4, 4, 14);
        if (result)
            cout << "Yes!
    ";
        else
            cout << "No!
    ";
    }
    
    void test3()//要查找的数是数组中最小的数字
    {
        cout << "Test3:
    ";
        int matrix[][4] = { {1, 2, 8, 9}, {2, 4, 9, 12}, {4, 7, 10, 13}, {6, 8, 11, 15} };
        bool result;
        result = serach_in(matrix[0], 4, 4, 1);
        if (result)
            cout << "Yes!
    ";
        else
            cout << "No!
    ";
    }
    
    void test4()//要查找的数是数组中最大的数字
    {
        cout << "Test4:
    ";
        int matrix[][4] = { {1, 2, 8, 9}, {2, 4, 9, 12}, {4, 7, 10, 13}, {6, 8, 11, 15} };
        bool result;
        result = serach_in(matrix[0], 4, 4, 15);
        if (result)
            cout << "Yes!
    ";
        else
            cout << "No!
    ";
    }
    
    void test5()//要查找的数比数组中最小的数字还小
    {
        cout << "Test5:
    ";
        int matrix[][4] = { {1, 2, 8, 9}, {2, 4, 9, 12}, {4, 7, 10, 13}, {6, 8, 11, 15} };
        bool result;
        result = serach_in(matrix[0], 4, 4, 0);
        if (result)
            cout << "Yes!
    ";
        else
            cout << "No!
    ";
    }
    
    void test6()//要查找的数比数组中最大的数字还大
    {
        cout << "Test6:
    ";
        int matrix[][4] = { {1, 2, 8, 9}, {2, 4, 9, 12}, {4, 7, 10, 13}, {6, 8, 11, 15} };
        bool result;
        result = serach_in(matrix[0], 4, 4, 16);
        if (result)
            cout << "Yes!
    ";
        else
            cout << "No!
    ";
    }
    
    void test7()//鲁棒性测试,输入空指针
    {
        cout << "Test6:
    ";
        bool result;
        result = serach_in(NULL, 0, 0, 1);
        if (result)
            cout << "Yes!
    ";
        else
            cout << "No!
    ";
    }
    
    int main()
    {
        test1();
        test2();
        test3();
        test4();
        test5();
        test6();
        test7();//重点,鲁棒性测试
    
        system("pause");
    }
  • 相关阅读:
    Hadoop源代码分析
    Java中如何把两个数组合并为一个
    数据库死锁的解决办法
    Java程序中解决数据库超时与死锁
    mysql 死锁检查
    JDBC事务和数据库事务嵌套的讨论 .
    嵌套事务和事务保存点的错误处理
    java string常见操作题
    java基本数据类型包装类
    java string
  • 原文地址:https://www.cnblogs.com/CJT-blog/p/10458767.html
Copyright © 2011-2022 走看看