Advertisement
JosepRivaille

X88461: Màxim i mínim d'un vector

Sep 18th, 2015
435
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.55 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5.  
  6. pair<int, int> find_vector(vector<int>& v) {
  7.   int tam = v.size();
  8.   pair<int, int> res;
  9.   res.first = res.second = v[0];
  10.   for (int i = 1; i < tam; i++) {
  11.     if (v[i] > res.first) res.first = v[i];
  12.     else if (v[i] < res.second) res.second = v[i];
  13.   }
  14.   return res;
  15. }
  16.  
  17.  
  18. int main() {
  19.   int n;
  20.   cin >> n;
  21.   vector<int> v(n);
  22.   for (int i = 0; i < n; ++i) cin >> v[i];
  23.   pair<int, int> res = find_vector(v);
  24.   cout << res.first << " " << res.second << endl;
  25. }
  26.  
  27. //JosepRivaille
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement