Advertisement
Guest User

rangemax.cc

a guest
Oct 22nd, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.33 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. using namespace std;
  5.  
  6. int somme_consecutifs_max(vector<int> vect);
  7. vector<size_t> lignes_max(vector<vector<int>> matrice);
  8. vector<vector<int>> tranches_max(vector<vector<int>> matrice);
  9.  
  10.  
  11. int main()
  12. {
  13.     vector<vector<int>> matrice{{0},{0},{0},{0}/* emplacements à valeurs de base */};
  14.                                
  15.     for (size_t i = 0 ; i < matrice.size() ; i++) {    //affichage de la matrice de base    
  16.         for (size_t k = 0 ; k < matrice[i].size() ; k++) {
  17.             cout << matrice[i][k] << " ";
  18.         }
  19.         cout << endl;
  20.     }
  21.     cout << endl;
  22.    
  23.     vector<vector<int>> resultat (tranches_max(matrice));
  24.    
  25.     for (size_t i = 0 ; i < resultat.size() ; i++) {   //affichage de la matrice de fin
  26.         for (size_t k = 0 ; k < resultat[i].size() ; k++) {
  27.             cout << resultat[i][k] << " ";
  28.         }
  29.         cout << endl;
  30.  
  31.     }
  32. }
  33.  
  34. int somme_consecutifs_max(vector<int> vect)
  35. {
  36.     int somme = 0;
  37.     int max = 0;
  38.    
  39.     for (size_t i = 0; i < vect.size(); i++) { //calcul des maximums consecutifs
  40.         if (vect[i] != 0) {
  41.             somme += vect[i];
  42.         }
  43.         else {
  44.             if (somme > max) {
  45.                 max = somme;
  46.             }
  47.             somme = 0;
  48.         }
  49.     }
  50.    
  51.     if (somme > max ) {
  52.         max = somme;
  53.     }
  54.     return max;
  55. }
  56.  
  57. vector<size_t> lignes_max(vector<vector<int>> matrice)
  58. {
  59.     vector<int> sommes(4);
  60.     int max = 0;
  61.    
  62.     for (size_t i = 0 ; i < matrice.size() ; i++)  { //calcul de la valeur de la ligne max
  63.         sommes[i] = somme_consecutifs_max(matrice[i]);
  64.         if (max < sommes[i]) {
  65.             max = sommes[i];
  66.         }
  67.     }
  68.    
  69.     vector<size_t> lignes_max;
  70.    
  71.     for (size_t i = 0 ; i < matrice.size() ; i++) //assignation du numéro des lignes max
  72.     {
  73.         if(sommes[i] == max) {
  74.            lignes_max.push_back(i);
  75.         }
  76.     }
  77.    
  78.     return lignes_max;
  79. }
  80.  
  81. vector<vector<int>> tranches_max(vector<vector<int>> matrice)
  82. {
  83.     vector<size_t> numero_ligne(lignes_max(matrice));
  84.     vector<vector<int>> resultat;
  85.    
  86.     for (size_t i = 0 ; i < numero_ligne.size() ; i++ ) { //mises en place des lignes max sur la matrice de fin
  87.         resultat.push_back(matrice[numero_ligne[i]]);
  88.     }
  89.    
  90.     return resultat;
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement