12345678910111213141516171819202122232425262728293031323334353637383940 |
- #include <iostream>
- void rotateArray(int* arr, int size, int shift) {
- shift = shift % size;
- if (shift < 0) {
- shift = (shift % size + size) % size;
- }
- int* temp = new int[size];
- for (int i = 0; i < size; ++i) {
- temp[(i + shift) % size] = arr[i];
- }
- for (int i = 0; i < size; ++i) {
- arr[i] = temp[i];
- }
- delete[] temp;
- }
- int main() {
- int arr[] = {1, 2, 3, 4, 5};
- int size = sizeof(arr) / sizeof(arr[0]);
- int shift = 2;
- rotateArray(arr, size, shift);
- std::cout << "{";
- for (int i = 0; i < size; ++i) {
- std::cout << arr[i];
- if (i < size - 1) {
- std::cout << ", ";
- }
- }
- std::cout << "}" << std::endl;
- return 0;
- }
|