jasonpogi1669

Print Middle Pairs of Elements at index i and index n - i + 1 using C++

Oct 3rd, 2021 (edited)
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.62 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. int main() {
  6.     int n;
  7.     cin >> n;
  8.     vector<int> a(n);
  9.     iota(a.begin(), a.end(), 1);
  10.     cout << "Full list: " << '\n';
  11.     for (auto& e : a) {
  12.         cout << e << " ";
  13.     }
  14.     cout << '\n' << "Starting from middle: " << '\n';
  15.     for(int i = 0; (n & 1 ? i <= n / 2 : i < n / 2); i++) {
  16.         if (n & 1 && i == 0) {
  17.             //cout << n / 2 - i << '\n';
  18.             continue;
  19.         } else {
  20.             //cout << n / 2 - i << " " << n / 2 + i  << '\n';
  21.             int from = n / 2 - i;
  22.             int to = n / 2 + i;
  23.             if (n % 2 == 0) {
  24.                 from -= 1;
  25.             }
  26.             cout << a[from] << " " << a[to] << '\n';
  27.         }
  28.     }
  29.     return 0;
  30. }
  31.  
Add Comment
Please, Sign In to add comment