#include <iostream> using namespace std; int* writeDataToTable(int* source, int* destination, int startIndex, int n){ int i=0; for(i; i<n; i++){ destination[i] = source[startIndex + i]; } return destination; } int power(int value){ int i=0; int result = 1; for(i;i<value;i++) result = result *2; return result; } int* mergeTables(int* tableA, int* tableB, int n){ int* tableToMerge = new int[n]; int i=0; for(i; i<n/2; i++) tableToMerge[i] = tableA[i]; i=0; for(i; i<n/2; i++) tableToMerge[i+n/2] = tableB[i]; return tableToMerge; } int* flipTwoElementTable(int* table){ int* flipped = new int[2]; flipped[0] = table[1]; flipped[1] = table[0]; return flipped; } void printTable (int* table, int n){ int i=0; for(i; i<n; i++){ if (i==n) cout<<table[i]; else cout<<table[i]<<" "; } } int* schuffle(int* tableToBeSchuffled, int n){ int* schuffledTableUp; int* schuffledTableDown; if(n > 2){ schuffledTableUp = new int[n/2]; writeDataToTable(tableToBeSchuffled, schuffledTableUp, 0, n/2); schuffledTableDown = new int[n/2]; writeDataToTable(tableToBeSchuffled, schuffledTableDown, n/2, n/2); schuffledTableUp = schuffle(schuffledTableUp, n/2); schuffledTableDown = schuffle(schuffledTableDown, n/2); } else{ return flipTwoElementTable(tableToBeSchuffled); } tableToBeSchuffled = mergeTables(schuffledTableDown, schuffledTableUp, n); return tableToBeSchuffled; } int main() { int n = 0; int t = 0; cin >> n; cin >> t; int* table = new int[power(n)]; int i = 0; for(i;i<power(n); i++) cin >> table[i]; i=0; for(i; i<t; i++) table = schuffle(table,power(n)); printTable(table, power(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 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 | #include <iostream> using namespace std; int* writeDataToTable(int* source, int* destination, int startIndex, int n){ int i=0; for(i; i<n; i++){ destination[i] = source[startIndex + i]; } return destination; } int power(int value){ int i=0; int result = 1; for(i;i<value;i++) result = result *2; return result; } int* mergeTables(int* tableA, int* tableB, int n){ int* tableToMerge = new int[n]; int i=0; for(i; i<n/2; i++) tableToMerge[i] = tableA[i]; i=0; for(i; i<n/2; i++) tableToMerge[i+n/2] = tableB[i]; return tableToMerge; } int* flipTwoElementTable(int* table){ int* flipped = new int[2]; flipped[0] = table[1]; flipped[1] = table[0]; return flipped; } void printTable (int* table, int n){ int i=0; for(i; i<n; i++){ if (i==n) cout<<table[i]; else cout<<table[i]<<" "; } } int* schuffle(int* tableToBeSchuffled, int n){ int* schuffledTableUp; int* schuffledTableDown; if(n > 2){ schuffledTableUp = new int[n/2]; writeDataToTable(tableToBeSchuffled, schuffledTableUp, 0, n/2); schuffledTableDown = new int[n/2]; writeDataToTable(tableToBeSchuffled, schuffledTableDown, n/2, n/2); schuffledTableUp = schuffle(schuffledTableUp, n/2); schuffledTableDown = schuffle(schuffledTableDown, n/2); } else{ return flipTwoElementTable(tableToBeSchuffled); } tableToBeSchuffled = mergeTables(schuffledTableDown, schuffledTableUp, n); return tableToBeSchuffled; } int main() { int n = 0; int t = 0; cin >> n; cin >> t; int* table = new int[power(n)]; int i = 0; for(i;i<power(n); i++) cin >> table[i]; i=0; for(i; i<t; i++) table = schuffle(table,power(n)); printTable(table, power(n)); return 0; } |