---
# System prepended metadata

title: APCS2020_07 骰子

---

# APCS2020_07 骰子 
## Sol1.
想法:
骰子由於對面相加爲 7，因此我們只需儲存其中上、前、右方之數字即可。向右翻轉時，原本的左方（7 - 右方數字）變爲新的上方，而原來的上方變爲新的右方，向前翻轉以此類推。
也可用struct
```cpp=
#include<bits/stdc++.h>
using namespace std;
int front[25], up[25], rightt[25], n ,m;
void turn_f(int a){
    int forn = front[a];
    front[a] = up[a];
    up[a] = 7 - forn;
}
void turn_r(int a){
    int righ = rightt[a];
    rightt[a] = up[a];
    up[a] = 7 - righ;
}
void change(int a,int b){
    swap(front[a], front[b]);
    swap(up[a], up[b]);
    swap(rightt[a], rightt[b]);
}
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cin >> n >> m;
    for(int i = 1;i <=n;i ++){
        front[i] = 4;
        up[i] = 1;
        rightt[i] = 2;
    }
    for(int i = 0;i < m;i ++){
        int a, b;
        cin >> a >> b;
        if (b == -1){
            turn_f(a);
        }else if (b == -2){
            turn_r(a);
        }else{
            change(a,b);
        }
    }
    for(int i = 1;i <= n;i ++){
        cout << up[i] << " ";
    }
    return 0;
}

```
## Sol2.
想法:
一樣存上、下、右
但就單純找規律
不討論骰子的性質
```cpp=
#include <iostream>
using namespace std;
 
int n, m, u[25], f[25], r[25], o[7] = {0, 6, 5, 4, 3, 2, 1};
 
int main() {
    cin >> n >> m;
    for (int i = 1; i <= n; i++){
        u[i] = 1;
        f[i] = 4;
        r[i] = 2;
    }
    for (int i = 0, a, b; i < m; i++){
        cin >> a >> b;
        if (b == -1){
            f[a] = o[f[a]];
            swap(u[a], f[a]);
        }
        else if (b == -2){
            r[a] = o[r[a]];
            swap(u[a], r[a]);
        }
        else{
            swap(u[a], u[b]);
            swap(f[a], f[b]);
            swap(r[a], r[b]);
        }
    }
    for (int i = 1; i <= n; i++){
        cout << u[i] << " ";
    }
    cout << endl;
}
```
PS:
其實我覺得Sol1.比較好想
Sol2.考試我應該寫不出來w