-
Notifications
You must be signed in to change notification settings - Fork 50
/
11-6.cpp
60 lines (45 loc) · 1.01 KB
/
11-6.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <iostream>
#include <vector>
using namespace std;
void findEle(const vector<vector<int> > &matrix,int x,int &row,int &col){
int nrow = matrix.size();
if (nrow==0){return;}
int ncol = matrix[0].size();
int i=0;
int j=ncol-1;
while (i<nrow && j>=0){
if (matrix[i][j]==x){
row=i;
col=j;
return;
}else{
if (matrix[i][j]<x){
i++;
}else{
j--;
}
}
}
}
int main(){
int A[4][4] = {{15,20,40,85},
{20,35,80,95},
{30,55,95,105},
{40,80,100,120}};
vector<vector<int> > matrix(4,vector<int>(4,0));
cout << "Matrix: " << endl;
for (int i=0;i<4;i++){
for (int j=0;j<4;j++){
matrix[i][j]=A[i][j];
cout << matrix[i][j] << " " ;
}
cout << endl;
}
int row=-1;
int col=-1;
int x = 55;
cout << "Finding element: "<<x << endl;
findEle(matrix,x,row,col);
cout << "matrix[" << row+1 << "][" << col+1 <<"]=" << x<<endl;
return 0;
}