Finds an integer in a two-dimensional array in which each row increments from left to right in each column from top to bottom--3

Source: Internet
Author: User

Given a two-dimensional array, the characteristics of the array is: Each row from left to right data size increments sequentially, each column of data from top to bottom in order to determine whether an integer in this two-dimensional array;

Design a two-dimensional array as follows:

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/7F/73/wKioL1cfMmLxgqRwAAAY1Yn6gfs969.png "title=" arr[5] [5].png "alt=" Wkiol1cfmmlxgqrwaaay1yn6gfs969.png "/>


First of all, undoubtedly, the traversal of the array can certainly be judged, and this is the most stupid method, so to improve the efficiency of the program to find an efficient way to find;

At the beginning of the idea probably can think of from the first row of the first column of the array to determine the diagonal line, if it is diagonal data can be directly returned, for example, we want to find the number, this time is more than 0, 9, three is larger, so to 0, 9, 16 is the diagonal rectangle data can be ruled out, the next judge found more than the main stop, so the number of the card in a larger than 16 but less than 21 in the range, But this range can be drawn as:

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M01/7F/76/wKiom1cfOqaCNt4MAAAWK8Hh_nY360.png "title=" arr[5] [5].png "alt=" Wkiom1cfoqacnt4maaawk8hh_ny360.png "/>

As shown above, the data in the white area can be excluded, but the data that satisfies the judging condition so far is the data in the yellow area, because the array given above is given dead, but actually the data in the yellow region may satisfy the condition;

And then what? And then it got stuck ... This is not good to carry out again, so this idea will be blocked, you can change a way of thinking to start again;


First, you can start from the upper right corner of the array to judge, imagine, if the number to find than the upper right corner of the number is small, then the upper right corner of the column can be excluded, of course, if the upper right corner of the word is directly returned, and if the number to find more than the upper right corner of the larger, Then the upper right corner of the row can be ruled out, when the judgment after the exclusion of the row or the column is not within the scope of consideration, again looking for the rest of the upper right corner of the data, and then loop judgment, until the row from 0 to the maximum, the column from the maximum value changed to 0;

The code can be implemented as follows:

#include  <iostream> #include  <assert.h>using namespace std;bool searchnum ( int  (*parr) [5], size_t row, size_t col, int num) {     Assert (Parr);     size_t tmp_row = 0;    size_t tmp _col = col-1;    while (Tmp_row < row)  &&  (tmp_ col >= 0))      {            if (Parr[tmp_row][tmp_col] == num)              return true;        else if (parr[tmp_row][ Tmp_col] > num)             --tmp_col;         else             ++tMp_row;    }       return false;} Int main () {    int arr[5][5] = {{0,  1,  2,   3,  4},                       {5,  9, 10, 11, 12},                       {6, 13, 16, 17, 18},           &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;{7,&NBSP;14,&NBSP;19,&NBSP;21,&NBSP;24},                       {8, 15, 22, 23, 25}};    bool ret =  Searchnum (arr, 5, 5, 17);  &nbSp;//success return true, failed return false    if (ret)          cout<< "The num is exist ..." <<endl;     else        cout<< "the num is  Not exist ... "<<endl;    return 0;

When the number to find is 17, the running program results are as follows:

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M00/7F/74/wKioL1cfPz_x_2UJAAAFcMgV8No748.png "style=" float: none; "title=" Exist.png "alt=" Wkiol1cfpz_x_2ujaaafcmgv8no748.png "/>


When the number you want to find is changed to 20, the program runs as follows:

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/7F/77/wKiom1cfPnbDWWX4AAAFbUtybMc809.png "style=" float: none; "title=" Not Exist.png "alt=" Wkiom1cfpnbdwwx4aaafbutybmc809.png "/>



Finish

This article is from the "Knock Code good Sleep zzz" blog, please be sure to keep this source http://2627lounuo.blog.51cto.com/10696599/1767992

Finds an integer in a two-dimensional array in which each row increments from left to right in each column from top to bottom--3

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.