Search in two-dimensional array [Question 2], two-dimensional array search Question 2
-
Description:
-
In a two-dimensional array, each row is sorted in ascending order from left to right, and each column is sorted in ascending order from top to bottom. Complete a function, input a two-dimensional array and an integer to determine whether the array contains the integer.
-
Input:
-
The input may contain multiple test examples. For each test case,
The first row of the input is two integers, m and n (1 <= m, n <= 1000): the number of rows and columns of the matrix to be input.
The second line of the input contains an integer t (1 <= t <= 1000000): the number to be searched.
In the next m row, each row has n numbers, representing the matrix of n columns in the m row given by the question (the matrix is shown in the description of the question, and each row is sorted in ascending order from left to right, each column is sorted in ascending order from top to bottom.
-
Output:
-
Corresponding to each test case,
The output "Yes" indicates that the number t is found in the two-dimensional array.
The output "No" indicates that the number t is not 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
Poj address:
-
Http://ac.jobdu.com/problem.php? Pid = 1, 1384
-
AC code:
-
# Include <stdio. h>
# Include <iostream>
# Include <string. h>
# Include <fstream>
# Include <memory. h>
Using namespace std;
Int main ()
{
Int n, m, target;
// Ifstream cin ("input.txt ");
// While (cin> m)
While (scanf ("% d", & m, & n )! = EOF)
{
// Cin> n;
Bool found = false;
Int * data = new int [n * m];
// Cin> target;
Scanf ("% d", & target );
Int row, col;
For (row = 0; row <m; row ++ ){
For (col = 0; col <n; col ++ ){
// Cin> data [row * n + col];
Scanf ("% d", & data [row * n + col]);
}
}
Row = 0; col = n-1;
While (row <m & col> = 0 ){
If (data [row * n + col] = target ){
Found = true;
Break;
} Else if (data [row * n + col] <target ){
Row ++;
} Else {
Col --;
}
}
If (found ){
Printf ("Yes \ n ");
} Else {
Printf ("No \ n ");
}
Delete [] data;
}
// Getchar ();
Return 0;
}
/*************************************** ***********************
Problem: 1384
User: ycxmartin1111
Language: C ++
Result: Accepted
Time: 680 MS
Memory: 5428 kb
**************************************** ************************/
-
-
-