csansoon

P8.10 P42596 Control C401B

Nov 29th, 2018
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.17 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5. typedef vector<vector<int> > Matrix;
  6.  
  7. void minmax(const Matrix& m, int& max, int& min){
  8.     max = min = m[0][0];
  9.     for(int i = 0; i < m.size(); ++i){ //(^)
  10.         for(int j = 0; j < m[0].size(); ++j){ //<->
  11.             if(m[i][j] > max) max = m[i][j];
  12.             if(m[i][j] < min) min = m[i][j];
  13.         }
  14.     }
  15. }
  16.  
  17. int main(){
  18.     int r, c;
  19.     int max, min, dif;
  20.     max = min = 0;
  21.  
  22.     int number = 0;
  23.     int maxnum = 0; //Maxdif will tell the maximum difference.
  24.     dif = -1;
  25.  
  26.     while(cin >> r >> c){
  27.         Matrix m(r,vector<int>(c));
  28.         //We fill the matrix.
  29.         for(int i = 0; i < r; ++i){ //(^)
  30.             for(int j = 0; j < c; ++j){ //<->
  31.                 cin >> m[i][j];
  32.             }
  33.         }
  34.  
  35.         minmax(m, max, min);
  36.         ++number;
  37.  
  38.         if(max-min > dif){
  39.             dif = max-min;
  40.             maxnum = number;
  41.         }
  42.     }
  43.  
  44.     cout << "la diferencia maxima es " << dif << endl;
  45.     cout << "la primera matriu amb aquesta diferencia es la " << maxnum << endl;
  46.  
  47. }
  48.  
  49. // (c) Carlos Sansón (Best pro1 delegate ever for sure) @csansoon
Advertisement
Add Comment
Please, Sign In to add comment