-
题目描述:
-
在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。
-
输入:
-
输入可能包含多个测试样例,对于每个测试案例,
输入的第一行为两个整数m和n(1<=m,n<=1000):代表将要输入的矩阵的行数和列数。
输入的第二行包括一个整数t(1<=t<=1000000):代表要查找的数字。
接下来的m行,每行有n个数,代表题目所给出的m行n列的矩阵(矩阵如题目描述所示,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。
-
输出:
-
对应每个测试案例,
输出”Yes”代表在二维数组中找到了数字t。
输出”No”代表在二维数组中没有找到数字t。
-
样例输入:
-
3 351 2 34 5 67 8 93 312 3 45 6 78 9 103 3122 3 45 6 78 9 10
-
样例输出:
-
YesNoNo
http://ac.jobdu.com/problem.php?pid=1384
这个OJ系统特别怪,下面这个代码提交超时:
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
using namespace std;
int t;
int m,n;
int target;
int arr[1001][1001];
bool find(int matrix[1001][1001], int rows,int columns,int numbers)
{
if(matrix==0||rows<1||columns<1)
return false;
if(numbers<matrix[0][0]||numbers>matrix[rows-1][columns-1])
return false;
int row=0;
int column=columns-1;
while(row<rows&&column>=0)
{
if(matrix[row][column] == numbers)
return true;
else if(matrix[row][column] > numbers)
column--;
else
row++;
}
return false;
}
int main()
{
while(1)
{
cin>>m>>n>>target;
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
cin>>arr[i][j];
bool temp=find(arr,m,n,target);
if(temp == false)
printf("No\n");
else
printf("Yes\n");
}
return 0;
}
只要将while(1)改为while(cin>>m>>n>>target)就能AC了。