#include <stdio.h> #include <stdlib.h> #include <string.h> #include <math.h> static void memswap(void* ptr1, void* ptr2, size_t size) { void* buffer = malloc(size); memcpy(buffer, ptr1, size); memcpy(ptr1, ptr2, size); memcpy(ptr2, buffer, size); } static void tas(int* cards, int size) { if(size == 2) { memswap(cards, cards+1, sizeof(int)); return; } int half_size = size/2; int *second_part = cards + half_size; tas(cards, half_size); tas(second_part, half_size); memswap(cards, second_part, half_size*sizeof(int)); } int main(void) { int i,k,t; scanf("%d%d", &k, &t); int n = pow(2, k); int *numbers = (int*)malloc(n * sizeof(int)); for(i=0;i<n;++i) { scanf("%d", &numbers[i]); } if(t%2 == 1) tas(numbers, n); for(i=0;i<n;++i) { printf("%d ", numbers[i]); } printf("\n"); return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | #include <stdio.h> #include <stdlib.h> #include <string.h> #include <math.h> static void memswap(void* ptr1, void* ptr2, size_t size) { void* buffer = malloc(size); memcpy(buffer, ptr1, size); memcpy(ptr1, ptr2, size); memcpy(ptr2, buffer, size); } static void tas(int* cards, int size) { if(size == 2) { memswap(cards, cards+1, sizeof(int)); return; } int half_size = size/2; int *second_part = cards + half_size; tas(cards, half_size); tas(second_part, half_size); memswap(cards, second_part, half_size*sizeof(int)); } int main(void) { int i,k,t; scanf("%d%d", &k, &t); int n = pow(2, k); int *numbers = (int*)malloc(n * sizeof(int)); for(i=0;i<n;++i) { scanf("%d", &numbers[i]); } if(t%2 == 1) tas(numbers, n); for(i=0;i<n;++i) { printf("%d ", numbers[i]); } printf("\n"); return 0; } |