Created
July 7, 2013 14:29
-
-
Save luoxiaoxun/5943650 to your computer and use it in GitHub Desktop.
题目描述:
在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。 输入:
输入可能包含多个测试样例,对于每个测试案例, 输入的第一行为两个整数m和n(1<=m,n<=1000):代表将要输入的矩阵的行数和列数。 输入的第二行包括一个整数t(1<=t<=1000000):代表要查找的数字。 接下来的m行,每行有n个数,代表题目所给出的m行n列的矩阵(矩阵如题目描述所示,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。 输出:
对应每个测试案例, 输出”Yes”代表在二维数组中找到了数字t。 输出”No”代表在二维数组中没有找到数字t。 样例输入:3 3…
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (1)C++: | |
| #include<cstdio> | |
| using namespace std; | |
| bool find(int matrix[1000][1000],int rows,int columns,int number){ | |
| if(matrix!=NULL&&rows>0&&columns>0){ | |
| int rowIndex=0; | |
| int colIndex=columns-1; | |
| while(rowIndex<rows&&colIndex>=0){ | |
| if(matrix[rowIndex][colIndex]==number) return true; | |
| else if(matrix[rowIndex][colIndex]<number) rowIndex++; | |
| else colIndex--; | |
| } | |
| } | |
| return false; | |
| } | |
| int main(){ | |
| int m,n; | |
| int matrix[1000][1000]; | |
| while(scanf("%d%d",&m,&n)!=EOF){ | |
| int number; | |
| scanf("%d",&number); | |
| for(int i=0;i<m;i++) | |
| for(int j=0;j<n;j++) | |
| scanf("%d",&matrix[i][j]); | |
| printf("%s\n",find(matrix,m,n,number)==true?"Yes":"No"); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment