#include void shiftk(int *a, int n, int k); void invert(int* a, int n); int main() { FILE* in = fopen("input.txt", "r"); if (in == NULL) { perror("Cannot open input file"); return -1; } int x; int n = 0; while (fscanf(in, "%d", &x) == 1) { ++n; } fseek(in, 0, SEEK_SET); if (n == 0) { fprintf(stderr, "Empty input file.\n"); return -1; } --n; int *a = NULL; if (n > 0) a = new int[n]; int k; if (fscanf(in, "%d", &k) < 1) { fprintf(stderr, "Incorrect input file.\n"); return -1; } int m = 0; for (int i = 0; i < n; ++i) { if (fscanf(in, "%d", &(a[i])) == 1) { ++m; } } fclose(in); shiftk(a, m, k); FILE* out = fopen("output.txt", "w"); if (in == NULL) { perror("Cannot open output file"); return -1; } for (int i = 0; i < m; ++i) { if (i > 0) { if (i % 10 == 0) fprintf(out, "\n"); else fprintf(out, " "); } fprintf(out, "%d", a[i]); } fprintf(out, "\n"); fclose(out); return 0; } void shiftk(int *a, int n, int k) { if (n <= 1) // Degenerated case return; // Reduce k to the interval 0..n-1 // using the periodicity k %= n; if (k == 0) return; if (k < 0) // Shift to the left on k positions k += n; // == shift to the right on n-k invert(a, n-k); // Invert the beginning of array of length n-k invert(a+n-k, k); // Invert the end of array of length k invert(a, n); // Invert the entire array } void invert(int* a, int n) { int i = 0; int j = n-1; while (i < j) { int tmp = a[i]; a[i] = a[j]; a[j] = tmp; ++i; --j; } }