题目:在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有这整数。
代码如下:
#include <iostream>
#include <vector>
using namespace std;
const int SIZE = 4;
int main()
{
vector<vector<int>> buf(SIZE, vector<int>(SIZE));
int findNum;
for (int i = 0; i < SIZE; ++i)
for (int j = 0; j < SIZE; ++j)
cin >> buf[i][j];
cin >> findNum;
int rows, cols;
rows = 0;
cols = SIZE - 1;
while (rows != SIZE && cols != -1)
{
if (buf[rows][cols] == findNum)
break;
else if (buf[rows][cols] > findNum)
cols -= 1;
else
rows += 1;
}
if (rows == SIZE || cols == -1)
cout << "No" << endl;
else
cout << "Yes:" << rows << " " << cols << endl;
}