Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "pch.h"
- #include <iostream>
- #include <vector>
- using namespace std;
- vector<char> v = { '1', '2', '3', '4', '5', '6', '7', '8' };
- char buff;
- void swap(int pos1, int pos2, int length) {
- for (int i = 0; i < length; i++) {
- buff = v[pos1 + i];
- v[pos1 + i] = v[pos2 + i];
- }
- }
- void swap_sections(int pos1, int pos2, int length) {
- if (pos2 - pos1 == length - pos2) {
- swap(pos1, pos2, pos2 - pos1);
- }
- else if (pos2 - pos1 > length - pos2) {
- swap(pos1, pos2, length - pos2);
- swap_sections(pos1 + length - pos2, pos2,length);
- }
- else if (pos2 - pos1 < length - pos2) {
- swap(pos1, pos1 + length - pos2, pos2 - pos1);
- swap_sections(pos1, pos2, pos1 + length - pos2);
- }
- }
- int main() {
- for (auto c : v) {
- cout << c;
- }
- cout << endl;
- swap_sections(0, 5, size(v));
- for (auto c : v) {
- cout << c;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement