Basic C Language Algorithm 32-Saddle Point, algorithm 32

Source: Internet
Author: User

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.

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.