Two-dimensional array --- maximum values of two numbers and

Source: Internet
Author: User

Problem: given a two-dimensional array, find two numbers to make them the largest, requiring these two numbers to be different rows and columns. Solution: converts a two-dimensional array to a one-dimensional array. The length of the one-dimensional array is N = ROW * COL. Assume that the two-dimensional array a [2] [3] = {1, 3, 1 },{ 2, 4, 5 }}; then its two-dimensional array is B [6] = {1, 3, 1, 2, 4, 5 }; in this case, B [I] = a [I/COL] [I % COL]. ROW = 2, COL = 3; for example, B [2] = a [2/3] [2% 3] = a [0] [2] = 1; with this link, you can do it. B [I] (I =, 2 ...... , N-1), respectively, and B [0]-B [N-1] to determine whether the conditions of "different rows and columns" are met, if this parameter is met, maxVal is obtained, compared with the previous maxVal, and a relatively large value is given to maxVal until the comparison is over, so that the maximum value can be obtained. The time complexity is O (N2 );

/* Problem: given a two-dimensional array, find two numbers to make them the largest, requiring these two numbers to be different rows and columns. Solution: converts a two-dimensional array to a one-dimensional array. The length of the one-dimensional array is N = ROW * COL. Assume that the two-dimensional array a [2] [3] = {1, 3, 1 },{ 2, 4, 5 }}; then its two-dimensional array is B [6] = {1, 3, 1, 2, 4, 5 }; in this case, B [I] = a [I/COL] [I % COL]. ROW = 2, COL = 3; for example, B [2] = a [2/3] [2% 3] = a [0] [2] = 1; with this link, you can do it, B [I] (I =, 2 ......, N-1), respectively, and B [0]-B [N-1] to determine whether the conditions of "different rows and columns" are met, if this parameter is met, maxVal is obtained, compared with the previous maxVal, and a relatively large value is given to maxVal until the comparison is over, so that the maximum value can be obtained. The time complexity is O (N2 ); */# include <stdio. h> # include <stdlib. h> # define ROW 2 // number of rows # define COL 3 // Number of columns // typedef double ArrayType; int main (void) {int a [ROW] [COL] = {1, 3, 1}, {2, 4, 5}; // The given two-dimensional array int iterx = 0, itery = 0; // used to control the cyclic count int N = ROW * COL; // The length of the one-dimensional array int maxVal = 0; // store the maximum value int maxsu BF = 0, maxsubS = 0; // F: First number S: Second number int x = 0, y = 0; // Save the positions of two numbers for (iterx = 0; iterx <N; iterx ++) {for (itery = 0; itery <N; itery ++) {maxsubF = a [iterx/COL] [iterx % COL]; // The first number F maxsubS = a [itery/COL] [itery % COL]; // The second number S // interpretation of different columns in different rows, if (maxsubF + maxsubS> maxVal & (iterx/COL! = Itery/COL) & (iterx % COL! = Itery % COL) {maxVal = maxsubF + maxsubS; x = iterx; y = itery ;}} printf ("The MAX sum is % d \ n", maxVal ); printf ("first NUM: a [% d] [% d] = % d, Second NUM: a [% d] [% d] = % d \ n ", x/COL, x % COL, a [x/COL] [x % COL], y/COL, y % COL, a [y/COL] [y % COL]); return 0 ;}

 

 

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.