Sword refers to offer series source code--Find in two-D array

Source: Internet
Author: User

Topic 1384: Finding in a two-dimensional array

time limit:1 seconds

Memory limit:32 MB

Special question: No

submitted:14827

Resolution:2888

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

#include <iostream> #include <stdio.h>using namespace Std;bool findnum (int* arr,int rows,int colums,int Number) {    if (arr==null| | rows<=0| | colums<=0) {        return false;    }    int row = 0,colum = colums-1;    while (row<rows&&colum>=0) {        if (arr[row*colums+colum]==number) {            return true;        } else if (arr[row*colums+colum]>number) {            --colum;        } else{            ++row;        }    }    return false;} int main () {    int rows,colums,number;    while (scanf ("%d%d", &rows,&colums)!=eof) {        scanf ("%d", &number);        int *arr = new Int[rows*colums];        for (int i=0;i<rows*colums;i++) {            scanf ("%d", &arr[i]);        }        if (FindNum (Arr,rows,colums,number)) {            printf ("%s\n", "yes");        } else{            printf ("%s\n", "No");        }    }    return 0;}


Sword refers to offer series source code--Find in two-D array

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.