# 【題解】APCS1081026-2 洗牌

【題目敘述】http://judge.epass2u.com/problem/APCS1081026-2 (無法使用)
【解題想法】模擬

⚠️注意：前九組測資Accepted，最後一組測資 WA

```#include <iostream>
#include <stack>
using namespace std;

int n, m, op;
int a[105]; // 1~n
int p[105];

void myPrint(){
cout << p[1];
for (int i=2; i<=n; i++)
cout << ' ' << p[i];
cout << '\n';
}

void myCopy(){
for (int i=1; i<=n; i++)
p[i] = a[i];
}

void f1(){
// 1. 「反」洗牌
// a: 1 2 3 4 5 6 7 8 9 10
// p: 1 6 2 7 3 8 4 9 5 10
for (int i=1; i<= n/2; i++){
a[i] = p[2*i - 1];
a[n/2+i] = p[2*i];
}
myCopy();
}
void f2(){
// 2. 「反」反洗牌
// a: 1 2 3 4 5 6 7 8 9 10
// p: 1 3 5 7 9 2 4 6 8 10
for (int i=1; i<=n/2; i++){
a[2*i - 1] = p[i];
a[2*i] = p[n/2 + i];
}
myCopy();
}

void f3(int k){
// 3. 「反」k-切牌(k = 3)
// a: 1 2 3 4 5 6 7 8 9 10
// p: 4 5 6 7 8 9 10 1 2 3
for (int i=1; i<=k; i++){
a[i] = p[n - k + i];
}
for (int i=k+1; i<=n; i++){
a[i] = p[i - k];
}
myCopy();
}

int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
for (int i=1; i<=n; i++){
cin >> p[i];
}
stack<int> stk;
for (int i=0; i<m; i++){
cin >> op;
stk.push(op);
}
while (stk.size()){
op = stk.top();
stk.pop();
//1表示洗牌, 2表示反洗牌, 其他 k 表示 k-切牌
if (op == 1) f1();
else if (op == 2) f2();
else if (op >= 3 && op < n) f3(op);
}
myPrint();
return 0;
}
```

Python code (credit: Amy Chou)
⚠️注意：前九組測資Accepted，最後一組測資 WA

```n, m = map(int, input().split())
p = [0]
p.extend(list(map(int, input().split())))
op = list(map(int, input().split()))
a = [0 for _ in range(n+1)]
for i in range(m-1, -1, -1):
if op[i] == 1:
for j in range(1, n//2 + 1):
a[j] = p[2*j - 1]
a[n//2 + j] = p[2*j]
elif op[i] == 2:
for j in range(1, n//2 + 1):
a[2*j - 1] = p[j]
a[2*j] = p[n//2 + j]
else:
k = op[i]
for j in range(1, k+1):
a[j] = p[n-k+j]
for j in range(k+1, n+1):
a[j] = p[j-k]

for j in range(1, n+1):
p[j] = a[j]

print(p[1], end='')
for i in range(2, n+1):
print(' ', p[i], sep='', end='')
print()
```