递增矩阵的查找

确定一个数是否存在于递增型矩阵中:

 

bool FindInMatrixFromTopRightCorner(int(*Matrix)[5], int Row, int Find)
{
    int i = 0, j = 4, tmp = Matrix[0][4];
    while (i <= 4)
    {
        if (Find > Matrix[i][j])
            i++;
        else if (Find == tmp)
        {
            cout << "Row : " << i << endl << "Line : " << j << endl;
            return true;
        }

        else
        {
            while (j >= 0)
            {
                if (Find < Matrix[i][j])
                    --j;
                else if (Find == Matrix[i][j])
                {
                    cout << "Row : " << i << endl << "Line : " << j << endl;;
                    return true;
                }
                else
                    return false;
            }
            return false;
        }
    }
    return false;
}
右上角开始查找
bool FidnInMatrixFromBottomLeftCorner(int(*Matrix)[5], int Row, int Find)
{
    int i = Row - 1, j = 0;
    while (i >= 0)
    {
        if (Find < Matrix[i][j])
            i--;
        else if (Find == Matrix[i][j])
        {
            cout << "Row : " << i << endl << "Line : " << j << endl;
            return true;
        }
        else
        {
            while (j <= 4)
            {
                if (Find > Matrix[i][j])
                    j++;
                else if (Find == Matrix[i][j])
                {
                    cout << "Row : " << i << endl << "Line : " << j << endl;
                    return true;
                }
                else
                    return false;
            }
            return false;
        }
    }
    return false;
}
左下角开始查找

 

posted @ 2016-07-04 19:22  _in_the_way  阅读(328)  评论(0编辑  收藏  举报