Advertisement
Guest User

Untitled

a guest
Nov 26th, 2014
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.90 KB | None | 0 0
  1. #include <fstream>
  2. using namespace std;
  3.  
  4. void shift_mas (int mas[], int n, int k){
  5.     int first_index, second_index, temp_first, temp_second;
  6.     first_index = 0;
  7.     temp_first = mas[first_index];
  8.     for (int i = 0; i < n; i++){
  9.         second_index = ( first_index + k ) % n;
  10.         temp_second = mas[second_index];
  11.         mas[second_index] = temp_first;
  12.         temp_first = temp_second;  
  13.         first_index = second_index;
  14.     }  
  15. }
  16.  
  17. void print_mas(int mas[], int n){
  18.     ofstream out ("output.txt");
  19.     for (int i = 0; i < n; i++){
  20.         out << mas[i] << " ";
  21.     }
  22.     out.close();
  23. }
  24.  
  25. int main () {
  26.     ifstream in("input.txt");
  27.     const int N_MAX = 100000;
  28.     int n, k, mas[N_MAX];
  29.     in >> n;
  30.     for (int i = 0; i < n; i++) in >> mas[i];
  31.     in >> k;
  32.     if ( k >= 0 ) k %= n; else k = n - (-k) % n;
  33.     shift_mas(mas, n, k);
  34.     print_mas(mas, n);
  35.     in.close();
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement