csansoon

P7.14 X12559 Reorganize a vector

Nov 14th, 2018
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.45 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. using namespace std;
  5.  
  6. // Returns a vector of size n > 0 with n integer values read from cin.
  7. vector<int> read_vector(int n) {
  8.     vector<int> v(n);
  9.     for (int i = 0; i < n; ++i)
  10.        cin >> v[i];
  11.     return v;
  12. }
  13.  
  14. // Prints a vector of size n > 0 in the cout.
  15. void print_vector(const vector<int>& v) {
  16.     for (int i = 0 ; i < int(v.size()) ; ++i) {
  17.         if (i>0) cout<<" ";
  18.         cout<<v[i];
  19.     }
  20.     cout<<endl;
  21. }
  22.  
  23. // add functions or procedures here if you need them
  24.  
  25. // see the statement of the problem
  26. vector<int> reorganize_vector(const vector<int>& v,int a,int b)
  27. {
  28.     // Write your implementation here.
  29.     int n = v.size();
  30.     vector<int> new_vect(n);
  31.     int index=0;
  32.     for (int i = 0; i < n; ++i) {
  33.         if (v[i] < a) {
  34.             new_vect[index] = v[i];
  35.             ++index;
  36.         }
  37.     }
  38.     for (int i = 0; i < n; ++i) {
  39.         if (v[i] >= a and v[i] <= b) {
  40.             new_vect[index] = v[i];
  41.             ++index;
  42.         }
  43.     }
  44.     for (int i = 0; i < n; ++i) {
  45.         if (v[i] > b) {
  46.             new_vect[index] = v[i];
  47.             ++index;
  48.         }
  49.     }
  50.  
  51.     return new_vect;
  52. }
  53.  
  54. int main() {
  55.     int n;
  56.     while (cin >> n) {
  57.          vector<int> v = read_vector(n);
  58.          int a, b;
  59.          cin >> a >> b;
  60.          print_vector(reorganize_vector(v, a, b));
  61.    }
  62. }
  63.  
  64. // (c) Carlos Sansón (Best pro1 delegate ever for sure) @csansoon
Advertisement
Add Comment
Please, Sign In to add comment