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 ;}