Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Find value in sorted matrix
//Title of this code #include <iostream> using namespace std; const int m = 3; const int n = 4; int binarySearch(int t[m][n], int row, int val) { int begin = 0; int end = n - 1; while (begin < end) { int mid = (end - begin) / 2 + begin; if (val < t[row][mid]) end = mid - 1; else if (val > t[row][mid]) begin = mid + 1; else { begin = mid; break; } } if (t[row][begin] == val) return begin; return -1; } void searchValueInMatrix(int t[m][n], int val) { for (int row = 0; row < m; ++row) { int col = binarySearch(t, row, val); if (col > -1) cout << row << "," << col << " "; } } int main() { int t[m][n] = { {1, 2, 9, 20}, {8, 9, 10, 21}, {9, 13, 14, 25} }; searchValueInMatrix(t, 25); }
run
|
edit
|
history
|
help
0
多态
My First Wall
Fractional Knapsack
325324
Mirror a binary tree
Left view of a tree
pangram
Client
Backpack with recursion
ammmma