Advertisement
Taraxacum

Saddle_Point

Oct 17th, 2018
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.90 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. #define MAT_SIZE 5
  6. int mat[MAT_SIZE][MAT_SIZE] = { 11, 3, 5, 6, 9, 12, 4, 7, 8, 10, 10, 5, 6, 9, 11, 8, 6, 4, 7, 2, 15, 10, 11, 20, 25 };
  7.  
  8. // #define MAT_SIZE 3
  9. // int mat[MAT_SIZE][MAT_SIZE] = { 12, 34, 23, 34, 45, 34, 35, 45, 34 };
  10.  
  11. bool Solution1()
  12. {
  13.     bool found = false;
  14.  
  15.     for (int i = 0; i < MAT_SIZE; i++) {
  16.         int max_j = 0;
  17.         int min_i = 0;
  18.         bool flag = true;
  19.  
  20.         for (int j = 1; j < MAT_SIZE; j++) {
  21.             if (mat[i][j] > mat[i][max_j]) {
  22.                 max_j = j;
  23.             }
  24.         }
  25.  
  26.         for (int k = 0; k < MAT_SIZE; k++) {
  27.             if (k == i) {
  28.                 continue;
  29.             } else if (mat[k][max_j] < mat[i][max_j]) {
  30.                 flag = false;
  31.                 break;
  32.             }
  33.         }
  34.  
  35.         if (flag) {
  36.             found = true;
  37.             cout << i << '\t' << max_j << '\t' << mat[i][max_j] << endl;
  38.         }
  39.     }
  40.  
  41.     return found;
  42. }
  43.  
  44. bool Solution2()
  45. {
  46.     bool found = false;
  47.  
  48.     int row_max[MAT_SIZE] = { 0 };
  49.     int col_min[MAT_SIZE] = { 0 };
  50.  
  51.     for (int i = 0; i < MAT_SIZE; i++) {
  52.         for (int j = 1; j < MAT_SIZE; j++) {
  53.             if (mat[i][j] > mat[i][row_max[i]]) {
  54.                 row_max[i] = j;
  55.             }
  56.         }
  57.     }
  58.  
  59.     for (int j = 0; j < MAT_SIZE; j++) {
  60.         for (int i = 1; i < MAT_SIZE; i++) {
  61.             if (mat[i][j] < mat[col_min[j]][j]) {
  62.                 col_min[j] = i;
  63.             }
  64.         }
  65.     }
  66.  
  67.     for (int k = 0; k < MAT_SIZE; k++) {
  68.         if (col_min[row_max[k]] == k) {
  69.             found = true;
  70.             cout << k << '\t' << row_max[k] << '\t' << mat[k][row_max[k]] << endl;
  71.         }
  72.     }
  73.  
  74.     return found;
  75. }
  76.  
  77. int main()
  78. {
  79.     if (!Solution1()) {
  80.         cout << "Not Found" << endl;
  81.     }
  82.  
  83.     if (!Solution2()) {
  84.         cout << "Not Found" << endl;
  85.     }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement