Search in two-dimensional array

Source: Internet
Author: User
This question is awkward. c ++ has timed out... Later, the main function was changed to C...

Algorithm:

/* Description: in a two-dimensional array, each row is sorted in ascending order from left to right, and each column is sorted in ascending order from top to bottom. Complete a function, input a two-dimensional array and an integer to determine whether the array contains the integer. Input: the input may contain multiple test examples. For each test case, the first input behavior is two integers, M and N (1 <= m, n <= 1000 ): the number of rows and columns of the matrix to be input. The second line of the input contains an integer T (1 <= T <= 1000000): the number to be searched. In the next m row, each row has n numbers, representing the matrix of n columns in the m row given by the question (the matrix is shown in the description of the question, and each row is sorted in ascending order from left to right, each column is sorted in ascending order from top to bottom. Output: For each test case, "yes" indicates that the number t is found in the two-dimensional array. The output "no" indicates that the number t is not 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 ----------------------------------------------------- principle: select the number in the upper-right corner of the array for comparison. If the number is larger than the number to be searched, delete the column. If the number is smaller than the number to be searched, delete the row; in this way, the two-dimensional array can be reduced by one row or one column each time, and the element can be found. */# Include <iostream> # include <stdlib. h> # include <stdio. h> using namespace STD; # define Max 1000000 bool find (INT arr [], int row, int column, int target) {int R, C; if (ARR = NULL | row <1 | column <1) {return false;} r = 0, c = column-1; while (r <= row-1 & C> = 0) {If (target = arr [R * column + C]) {return true ;} else if (target> arr [R * column + C]) {r ++;} else {c -- ;}} return false ;} int main () {int m, n; while (scanf ("% d", & M, & N )! = EOF) {int key, I; static int matrix [Max] = {0}; scanf ("% d", & Key); for (I = 0; I <m * n; I ++) scanf ("% d", matrix + I); bool result = find (matrix, M, N, key); If (result) printf ("Yes \ n"); else printf ("NO \ n");} return 0 ;}


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.