Title Description
(The length of each one-dimensional array of same), each row from left to right in order of ascending sort, to sort each column in a two-dimensional array in order of increasing from top to bottom. A complete function, enter such a two-dimensional array and an integer, it is determined whether the array contains the integer.
C ++ 11 realized
#include <iostream> #include <vector> #include <algorithm> #include <string> using namespace std; bool Find(vector<vector<int> > array,int target) { int rowCount = array.size(); int colCount = array[0].size(); int i,j; for(i=rowCount-1,j=0; i>=0&&j<colCount;) { if(target == array[i][j]) return true; if(target < array[i][j]) { i--; continue; } if(target > array[i][j]) { j++; continue; } } return false; } int main() { int k = 0; string sval; vector<int> sVec; vector<vector<int>> pVec; for(int j = 0; j < 5; ++j) { for(int i = k; i < 10+k; ++i) sVec.push_back(i); k++; cout << "Child vector "<< sVec.size() << endl; for(vector<int>::iterator itor = sVec.begin(); itor != sVec.end(); itor++) cout << *itor << endl; pVec.push_back(sVec); sVec.clear(); } cout << "Parent vector "<< pVec.size() << endl; do { cout <<"Please input the check integer" << endl ; cin >> sval; if(!Find(pVec,stoi(sval))) cout << "Not find the integer data "<< endl; else cout << "Find the integer data "<< endl; } while(1); return 0; }
思路
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
| 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 |
| 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 |
| 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 |
Starting with the last line of the first number 4 begin retrieving i = rowCount-1, j = 0, n is assumed to be m and the target value comparison.
1. m <n, naturally without increasing the number of rows, to increase the number of queries to the right column
2. m> n, a natural number of rows up query
3. m == n, has been found, quit
The subject lacks technical content Kazakhstan