PanaPatryk44

max rekurencja

Oct 17th, 2021
816
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. int max(std::vector<int> vec) {
  2.     if (vec.size() == 2) {
  3.         if (vec[0] > vec[1]) {
  4.             return vec[0];
  5.         }
  6.         return vec[1];
  7.     }
  8.  
  9.     int last = vec.back();
  10.     vec.pop_back();
  11.     int new_max = max(vec);
  12.     if (last > new_max) {
  13.         return last;
  14.     }
  15.     return new_max;
  16.  
  17.  
  18. }
  19.  
  20.  
  21. int main() {
  22. // std::cout<<max<int,int,int,int>(1,2,3,4, [](int lhs, int rhs){
  23. //     return  lhs>rhs;
  24. // } );
  25.     std::cout << max({4, 2, 3, 1}) << '\n';
  26.     std::cout << max({4, 5, 3, 1}) << '\n';
  27. }
RAW Paste Data