Topic 1384: Finding in a two-dimensional array
-
time limit:1 seconds
Memory limit:32 MB
Special question: No
submitted:17921
Resolution:3452
-
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
-
-
Answering:
-
- problem solving problems? to discuss the subject of discussion please visit: http://t.jobdu.com/thread-8107-1-1.html
-
-
- this question in the sword refers to the offer on a question, the algorithm idea is, from the back forward, judging the upper right corner of the array of the number, you can, I use a C + + vector to represent the array, may be the input problem, has not AC
-
-
#include <iostream> #include <vector>using namespace Std;bool IsFound2 (vector<vector<int> > &ary,int X,int Y,int a) {for (int. j=y-1;j>0;j--) {if (ary[0][j]==a) return 1;if (A>ary[0][j]) {for (int i=1;i<x ; i++) if (A==ary[i][j]) return 1;}} return 0;} int main () {int x=0,y=0;int a=0;while (cin>>x>>y) {Cin>>a;cin.sync ();vector<vector<int> > ary;vector<int> vec;for (int i=0;i<x;i++) {for (int j=0;j<y;j++) {int b;cin>>b;vec.push_back (b);} Ary.push_back (VEC); Vec.clear ();} if (IsFound2 (ary,x,y,a) ==true) cout<< "Yes" <<endl;elsecout<< "No" <<endl;}}
jdoj_1384--Array Lookup