Advertisement
wojiaocbj

rotate

Mar 21st, 2022
1,103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  Author: 曹北健(37509)
  3.  Result: AC Submission_id: 4204005
  4.  Created at: Mon Mar 21 2022 11:08:38 GMT+0800 (China Standard Time)
  5.  Problem: 5416  Time: 259   Memory: 2044
  6. */
  7.  
  8. #include <stdio.h>
  9. unsigned int a[114514] = { 0 };
  10. void reverse_array_u32(unsigned int *arr, int l, int r){
  11.     int i = l, j = r, t;
  12.     while(i < j){
  13.         t = arr[i];arr[i] = arr[j];arr[j] = t;
  14.         i++;j--;
  15.     }
  16. }
  17. void array_rotate_left_u32(unsigned int *arr, int n, int k){
  18.     reverse_array_u32(arr, 0, k - 1);
  19.     reverse_array_u32(arr, k, n - 1);
  20.     reverse_array_u32(arr, 0, n - 1);
  21. }
  22. void array_rotate_right_u32(unsigned int *arr, int n, int k){
  23.     reverse_array_u32(arr, 0, n - 1);
  24.     reverse_array_u32(arr, 0, k - 1);
  25.     reverse_array_u32(arr, k, n - 1);
  26. }
  27. int main(){
  28.     int n, i, k, tmp, bittomove, numtomove;
  29.     char dir[5] = { 0 };
  30.     unsigned int t, b0;
  31.     while(~scanf("%d", &n)){
  32.         for(i = 0;i < n;i++){
  33.             scanf("%d", &tmp);
  34.             a[i] = (unsigned int)tmp;
  35.         }
  36.         scanf("%s%d", dir, &t);
  37.         bittomove = t % 32;
  38.         numtomove = (t / 32) % n;
  39.         if(dir[0] == 'l'){
  40.             if(bittomove){
  41.                 b0 = a[0] >> (32 - bittomove);
  42.                 for(k = 0;k < n - 1;k++){
  43.                     a[k] = (a[k] << bittomove) | (a[k + 1] >> (32 - bittomove));
  44.                 }
  45.                 a[n - 1] = a[n - 1] << bittomove | b0;
  46.             }
  47.             array_rotate_left_u32(a, n, numtomove);
  48.         }
  49.         else{
  50.             if(bittomove){
  51.                 b0 = a[n - 1] & ((1 << bittomove) - 1);
  52.                 for(k = n - 1;k > 0;k--){
  53.                     a[k] = (a[k] >> bittomove) | ((a[k - 1] & ((1 << bittomove) - 1)) << (32 - bittomove));
  54.                 }
  55.                 a[0] = (a[0] >> bittomove) | (b0 << (32 - bittomove));
  56.             }
  57.             array_rotate_right_u32(a, n, numtomove);
  58.         }
  59.         for(i = 0;i < n;i++){
  60.             printf("%d ", (int)(a[i]));
  61.         }
  62.         putchar('\n');
  63.     }
  64.     return 0;
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement