Basic C Language Algorithm 32-Saddle Point, algorithm 32
// Array
/*
========================================================== ======================================
Question: Find all the saddle points of any matrix! And count! (In a matrix, a number is the maximum value in the row,
The minimum value in the column is called the saddle point .)
For example:
1 2 3
4 5 6
7 8 9
A [1] [3] = 3 is the unique Saddle Point of the matrix.
========================================================== ======================================
*/
# Include <stdio. h>
# Define N 300
Void main ()
{
Int I, j, k, s = 0;
Int c, r, m, n;
Int a [N] [N];
Printf ("input phalanx size: \ n rows = ");
Scanf ("% d", & m );
Printf ("column = ");
Scanf ("% d", & n );
Printf ("input % d * % d matrix: \ n", m, n );
For (I = 0; I <m; I ++)
For (j = 0; j <n; j ++)
Scanf ("% d", & a [I] [j]);
Printf ("\ n ===============================\ n matrix shape: \ n ");
For (I = 0; I <m; I ++)
{
For (j = 0; j <n; j ++)
Printf ("% 4d", a [I] [j]);
Printf ("\ n ");
}
Printf ("\ n ===============================\ n Saddle Point: \ n ");
For (I = 0; I <m; I ++)
{
C = 0;
For (j = 0; j <n; j ++)
If (a [I] [j]> a [I] [c]) c = j;
R = 0;
For (k = 0; k <m; k ++)
If (a [k] [c] <a [r] [c]) r = k;
If (r = I)
{
S ++;
Printf ("a [% d] [% d] = % 2d", r + 1, c + 1, a [r] [c]);
}
}
Printf ("\ n % d Saddle Points \ n", s );
}
/*
========================================================== ======================================
Rating:
Key Point: Find the largest element in each row, and use the for loop to control the row, which is equivalent to finding the maximum number in a row, but now
Calculate the maximum element of all rows in a loop! Record the number of columns in Column c. c is dynamic and is known in the column.
(It is already c), calculate the smallest element in the column, and record the row in r. If this row matches the column's most
If the number of rows corresponding to a small element is equal, this is a saddle point and repeats until all possible points are enumerated!
========================================================== ==================================
*/
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.