Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- using namespace std;
- /**
- * Циклически сдвиньте элементы массива вправо на заданное количество позиций s.
- * (A[0] переходит на место A[s-1], A[1] на место A[s], ..., последний элемент переходит на место A[s-2]).
- * Используйте минимально возможное количество операций присваивания. Не используйте дополнительный массив
- * @param arr
- * @param length
- * @param shift
- * @return
- */
- void shift_array(int* arr, const int length, const int shift)
- {
- int lastindex = 0;
- int indexsolve = 0;
- int a = 0;
- int b = 0;
- int countofoper = length;
- while (countofoper>1)
- {
- b = indexsolve - shift % length > 0 ? indexsolve - shift % length : (indexsolve - shift % length + length) ;
- b %= length;
- a = arr[b];
- arr[b] = arr[lastindex];
- arr[lastindex] = a;
- countofoper--;
- if (b == lastindex)
- {
- lastindex++;
- indexsolve = lastindex;
- }
- else
- {
- indexsolve = b;
- }
- }
- }
- int main()
- {
- int length, shift;
- int* array;
- std::ifstream file("input.txt");
- file >> length >> shift;
- array = new int[length];
- for (int i = 0; i < length; ++i)
- file >> array[i];
- shift_array(array, length, shift);
- file.close();
- std::ofstream output("output.txt");
- for (int i = 0; i < length; ++i)
- output << array[i] << ' ';
- output.close();
- delete[] array;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement