PAT 08-2 finding the local maximum of matrix, pat08-2

Source: Internet
Author: User

PAT 08-2 finding the local maximum of matrix, pat08-2

This question is quite simple, but it is an article every day. Two points: first, my carelessness led me to spend a lot of time looking for errors. I saw four test cases for three. I thought the boundary condition was not taken into account, or there is a hidden logic or syntax error. Through repeated comparisons with other programs, we can find that the four of my statements are the same as the two in the condition, and one is missing. The second is the tragedy, flag is a bit awkward. You can use cnt ++, but the logic is to express whether it is related. I still use bool. The question setting requirements and code implementation are as follows:

1/* 2 Name: 3 Copyright: 4 Author: 5 Date: 02/04/15 6 Description: 7 given M row N column integer matrix, if the non-Boundary Element A [I] [j] of A is greater than four adjacent upper, lower, and left elements, element A [I] [j] is the local maximum of the matrix. This question requires that all the local maximum values of the given matrix and their locations be given. 8 9 input format: 10 11 input the number of rows M and number of columns N (3 <= M, N <= 20) of matrix A in row 1st; the last M row, each row is given the value of A in N elements of the row. Numbers are separated by spaces. 12 13 Output Format: 14 15 each line outputs a local maximum value in the format of "element value row number column number", where the row and column number start from 1. Incremental output is required according to the row number. If the same row has more than one local maximum, the output is incremental by column number. If there is no local maximum value, "Total number of rows in None" is output ". 16 17 input sample 4 519 1 1 1 1 120 1 3 3 9 3 121 1 5 5 5 122 1 1 1 1 123 output sample 9 2 325 5 3 226 5 3 3 427 input sample 3 529 1 1 1 1 130 9 3 9 9 131 1 5 3 5 132 output sample None 3 534 */35 36 # include <stdio. h> 37 # include <stdbool. h> 38 39 void maxium (int M, int N); 40 41 int main () 42 {43 // freopen ("in.txt", "r", stdin ); // for test44 45 int M, N; 46 47 scanf ("% d", & M, & N); 48 49 maxium (M, N ); 50 51 // fclose (stdin); // for test52 53 return 0; 54} 55 56 void maxium (int M, int N) 57 {58 int I, j, a [M] [N]; 59 60 for (I = 0; I <M; I ++) 61 for (j = 0; j <N; j ++) 62 scanf ("% d", & a [I] [j]); 63 64 bool flag; 65 66 flag = true; 67 for (I = 1; I <M-1; I ++) 68 {69 for (j = 1; j <N-1; j ++) 70 {71 if (a [I] [j]> a [I-1] [j] & a [I] [j]> a [I] [j + 1] & a [I] [j]> a [I + 1] [j] & a [I] [j]> a [I] [j-1]) 72 {73 printf ("% d \ n", a [I] [j], I + 1, j + 1); 74 if (flag) 75 flag = false; 76} 77} 78} 79 if (flag) 80 printf ("None % d \ n", M, N); 81}

 

Related Article

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.