Advertisement
Guest User

Untitled

a guest
Dec 10th, 2019
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.86 KB | None | 0 0
  1. #include "pch.h"
  2. #include <iostream>
  3. #include <vector>
  4.  
  5. using namespace std;
  6.  
  7. vector<char> v = { '1', '2', '3', '4', '5', '6', '7', '8' };
  8. char buff;
  9.  
  10. void swap(int pos1, int pos2, int length) {
  11.  
  12. for (int i = 0; i < length; i++) {
  13. buff = v[pos1 + i];
  14. v[pos1 + i] = v[pos2 + i];
  15. }
  16. }
  17.  
  18. void swap_sections(int pos1, int pos2, int length) {
  19. if (pos2 - pos1 == length - pos2) {
  20. swap(pos1, pos2, pos2 - pos1);
  21. }
  22.  
  23. else if (pos2 - pos1 > length - pos2) {
  24. swap(pos1, pos2, length - pos2);
  25. swap_sections(pos1 + length - pos2, pos2,length);
  26. }
  27.  
  28. else if (pos2 - pos1 < length - pos2) {
  29. swap(pos1, pos1 + length - pos2, pos2 - pos1);
  30. swap_sections(pos1, pos2, pos1 + length - pos2);
  31. }
  32.  
  33.  
  34. }
  35.  
  36. int main() {
  37. for (auto c : v) {
  38. cout << c;
  39. }
  40. cout << endl;
  41. swap_sections(0, 5, size(v));
  42. for (auto c : v) {
  43. cout << c;
  44. }
  45. return 0;
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement