Topic 1384: Finding in a two-dimensional array
time limit:1 seconds
Memory limit:32 MB
Special question: No
submitted:14827
Resolution:2888
-
Title Description:
-
In a two-dimensional array, each row is ordered in ascending order from left to right, and each column is sorted in ascending order from top to bottom. Complete a function, enter a two-dimensional array and an integer to determine if the array contains the integer.
-
Input:
-
The input may contain multiple test samples, for each test case,
The first behavior of the input two integers m and n (1<=m,n<=1000): Represents the number of rows and columns of the matrix that will be entered.
The second line of input includes an integer t (1<=t<=1000000): Represents the number to find.
The next M-line, each row has n number, represents the title of the M-row N-column matrix (The matrix as shown in the title description, each row is ordered from left to right increments, each column is sorted from top to bottom in order.
-
Output:
-
corresponding to each test case,
The output "Yes" indicates that a number T is found in a two-dimensional array.
The output "No" indicates that no number T is found in the two-dimensional array.
-
Sample input:
-
3 351 2 34 5 67 8 93 312 3 45 6 78 9 103 3122 3 45 6 78 9 10
-
-
Sample output:
-
-
Yesnono
#include <iostream> #include <stdio.h>using namespace Std;bool findnum (int* arr,int rows,int colums,int Number) { if (arr==null| | rows<=0| | colums<=0) { return false; } int row = 0,colum = colums-1; while (row<rows&&colum>=0) { if (arr[row*colums+colum]==number) { return true; } else if (arr[row*colums+colum]>number) { --colum; } else{ ++row; } } return false;} int main () { int rows,colums,number; while (scanf ("%d%d", &rows,&colums)!=eof) { scanf ("%d", &number); int *arr = new Int[rows*colums]; for (int i=0;i<rows*colums;i++) { scanf ("%d", &arr[i]); } if (FindNum (Arr,rows,colums,number)) { printf ("%s\n", "yes"); } else{ printf ("%s\n", "No"); } } return 0;}
Sword refers to offer series source code--Find in two-D array