# 【題解】ZeroJudge e283: APCS 類似題 – 小崴的特殊編碼

【題目敘述】https://zerojudge.tw/ShowProblem?problemid=e283
【解題想法】將由0和1組成的四位數字（二進位）轉換成十進位數字，當作陣列的下標，即可速查對應的字元。

#### 【做法1】陣列與二進位數字

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

int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n;
while (cin >> n){
char letter[16];
letter[5] = 'A';
letter[7] = 'B';
letter[2] = 'C';
letter[13] = 'D';
letter[8] = 'E';
letter[12] = 'F';
for (int i=0; i<n; i++){
int x = 0, tmp;
for (int j=0; j<4; j++){
cin >> tmp;
x = 2 * x + tmp;
}
cout << letter[x];
}
cout << '\n';
}
return 0;
}
```

#### 【做法2】std::map

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

int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
map<string, char> mp;
mp["0 1 0 1"] = 'A';
mp["0 1 1 1"] = 'B';
mp["0 0 1 0"] = 'C';
mp["1 1 0 1"] = 'D';
mp["1 0 0 0"] = 'E';
mp["1 1 0 0"] = 'F';
unsigned int n;
string s, new_line;
while (cin >> n) {
//混用cin與getline時，注意cin之後，buffer中還殘留一個"\n"
getline(cin, new_line);
for (unsigned int i = 0; i < n; i++) {
getline(cin, s);
cout << mp[s];
}
cout << "\n";
}
return 0;
}
```

Python code (credit: Amy Chou)
【解題想法】直接用讀入的字串當 dictionary 的 key，減少計算時間。
【注意】使用 try – except 讀取測資會 TLE，改用 sys.stdin【筆記

```import sys
letter = {"0 1 0 1": "A",
"0 1 1 1": "B",
"0 0 1 0": "C",
"1 1 0 1": "D",
"1 0 0 0": "E",
"1 1 0 0": "F"}

for i in sys.stdin:
n = int(i)
ans = ""
for i in range(n):
s = sys.stdin.readline().strip()
ans += letter[s]
print (ans)
```