# 計算機韌體實驗
### c039
先找輸入i, j的最大值,接者執行 i~j 次尋找每一個介於 i~j 的 cycle length。
```cpp=
#include<iostream>
#include<algorithm>
using namespace std;
int main() {
int i, j, n = 0;
while (cin >> i >> j) {
int m = min(i, j);
int M = max(i, j);
int cycle = 0;
for (int k = m; k <= M; k++) {
int n = k;
int t = 0;
while (1) {
t++;
if (n == 1) break;
if (n % 2) n = 3 * n + 1;
else n = n / 2;
}
cycle = max(cycle, t);
}
cout << i << " " << j << " " << cycle << endl;
}
}
```
### c007
使用 `getline(cin , s)` 來一行一行讀取
接著使用 `for(auto c : s)` 以 char 的型態從頭讀取 s(字串),前面有 **auto** 代表可以直接修改 s(字串) 裡面的值。
利用 ? 條件判斷式 `(flag ? "flag 為 true 執行這裡" : "flag 為 flase 執行這裡")`。
```cpp=
#include<iostream>
#include<string>
using namespace std;
int main() {
string s;
bool flag = true;
while(getline(cin , s)){
for(auto c : s){
if(c == '"'){
cout << (flag ? "``" : "''");
flag = !flag;
} else cout << c;
}
cout << "\n";
}
}
```
### c054
`string s` 為鍵盤上從左到右的字母數字,透過讀取一筆一筆資料`cin.get(c)`,再判斷該 `c` 的的字母位於s[i] 的哪一個位置,如果有找到的話則往左位移1個單位。
```cpp=
#include<iostream>
using namespace std;
int main() {
string s;
char c;
int i;
s = "`1234567890-=QWERTYUIOP[]\\ASDFGHJKL;'ZXCVBNM,./";
while (cin.get(c)) {
for (i = 0; i < s.length() && c != s[i]; i++);
if (i < s.length()) {
cout << s[i - 1];
}
else cout << c;
}
}
```
### c543
迴文 -> `ABCDEDCBA`
鏡像 -> 從 `mt` 對照該字如果說reverse結果都一樣則該為鏡像
```cpp=
#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
#define ALL(x) x.begin(), x.end()
string nt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ123456789";
string mt = "A 3 HIL JM O 2TUVWXY51SE Z 8 ";
bool isP(string s);
bool isM(string s);
int main() {
string s;
while (cin >> s) {
bool p = isP(s);
bool m = isM(s);
cout << s << " -- is ";
if (p && m) cout << "a mirrored palindrome.";
else if (p) cout << "a regular palindrome.";
else if (m) cout << "a mirrored string.";
else cout << "not a palindrome.";
cout << "\n\n";
}
}
bool isP(string s) {
string rs = s;
reverse(ALL(rs));
return rs == s;
}
bool isM(string s) {
string rs = s;
reverse(ALL(rs));
for (int i = 0; i < rs.size(); i++) {
int pos = nt.find(rs[i]);
rs[i] = mt[pos];
}
return rs == s;
}
```
### d132
首先要先判斷如果輸入為 n 個 0 的話代表整個輸入結束,接著從 1~9 查看答案和輸入分別有幾個的值是一樣的,舉例來說 答案 `1 2 3 4`、輸入 `0 5 3 2`,我們可以知道 `2 3` 在答案裡有出現,所以 B 就 = 2,接著我們要減去完全正確的選項則 `B = B-A`。
```cpp=
#include<bits/stdc++.h>
using namespace std;
int main(){
int n, game = 0;
while(cin >> n && n != 0){
vector<int> s(n), g(n);
printf("Game %d:\n", ++game);
for(int i = 0; i<n; i++) cin >> s[i];
while(1){
int A = 0, B = 0, z = 0;
for(int i = 0; i<n; i++){
cin >> g[i];
if(s[i] == g[i]) A++;
if(g[i] == 0) z++;
}
if(n == z) break;
for(int i = 1; i<=9; i++){
int sc = 0, gc = 0;
for(int j = 0; j<n; j++){
if(i == s[j]) sc++;
if(i == g[j]) gc++;
}
B += min(sc, gc); // B = 1,
}
B -= A;
printf("\t(%d,%d)\n", A, B);
}
}
}
```
### e558
```cpp=
#include<bits/stdc++.h>
using namespace std;
int t[100001]= {0};
int main(){
// 建立查詢表
for(int n = 1; n <= 100001; n++){
int y = n, x = n;
while(x > 0){
y = y + x % 10;
x = x / 10;
}
if(y > 100001) continue;
if(t[y] == 0) t[y] = n;
}
// 開始解題
int T, m;
cin >> T;
while(T--){
cin >> m;
cout << t[m] << "\n";
}
}
```
### b123
先使矩形變為 0 1 1 1 -> 0 1 2 3
0 0 1 1 0 0 0 1 2 0
0 1 1 1 1 -> 0 1 2 3 4
0 1 1 1 1 0 1 2 3 4
0 0 1 0 0 0 0 1 0 0
```cpp=
#include<bits/stdc++.h>
using namespace std;
int main(){
int M, N;
while(cin >> M >> N){
int arr[201][201];
for(int i = 0; i<M; i++){
for(int j = 0; j<N; j++){
cin >> arr[i][j];
// 使矩形變為 0 1 1 1 -> 0 1 2 3
if(arr[i][j] && j>0) arr[i][j] = arr[i][j-1] + 1;
}
}
// 儲存最大矩形
int A = 0;
for(int i = 0; i<M; i++){ // 長
for(int j = 0; j<N; j++){ // 寬
int w = 201; // 題目最高的寬為200,超過200則為此題的最大值
for(int h = 0; h<=i && arr[i-h][j]; h++){ // i = 2 , j = 4
w = min(w, arr[i-h][j]); // 4
A = max(A, w*(h+1)); //
}
}
}
cout << A << "\n";
}
}
```
### j121
```cpp=
#include<bits/stdc++.h>
using namespace std;
#define ALL(x) x.begin(), x.end()
int main(){
string s1, s2;
while(cin >> s1 >> s2){
vector<int> v1(26, 0), v2(26, 0);
for(auto c : s1) v1[c - 'A']++; // JWPUDJSTVP
// 10,23,16,21,4,10,19,20,22,16
// v1[10] = 1, v1[23] = 1 ...
for(auto c : s2) v2[c - 'A']++; // VICTORIOUS
// 22,9,3,20,15,18,9,15,21,19
// v2[22] = 1, v2[9] = 1 ...
sort(ALL(v1));
sort(ALL(v2));
cout << (v1 == v2 ? "YES" : "NO") << "\n";
}
}
```
### e541
首先要先讀取大理石並排序,接著利用 lower_bound 直接升冪排列找要搜尋的數字
```cpp=
#include<bits/stdc++.h>
using namespace std;
#define ALL(x) x.begin(), x.end()
int main(){
int Case = 0;
int N, Q;
while(cin >> N >> Q && N != 0){
printf("CASE# %d:\n", ++Case);
// 讀取大理石並且排序
vector<int> v(N);
for(int i = 0; i<N;i++) cin >> v[i];
sort(ALL(v));
// 回答 Q 的問題
while(Q--){
int f;
cin >> f;
// lower_bound 找出 vector 中大於等於 f 的位置
// lower_bound 輸出為指標,減去 v.begin() 的位置轉為數值
int p = lower_bound(ALL(v), f) - v.begin();
if(p < N && v[p] == f) printf("%d found at %d\n", f, p+1);
else printf("%d not found\n", f);
}
}
}
```
### c073
- move 2 onto 1 : 將 1 以上的資料歸還給原本的值(ex: 1: 1 4 7 將 1 4 8 歸還到原本的位置) 再將 2 丟到 1 -> (1 : 1 2)
```cpp=
#include<bits/stdc++.h>
using namespace std;
int n; // 木塊數量
vector<int> v[25];
void find_block(int b, int &p, int &h); // 找木塊位置
void clear_above(int p, int h); // 指定木塊的位置
void pile_over(int p1, int h, int p2); // 移動木塊
void show();
int main(){
string s1, s2;
int a, b, pa, pb, ha, hb;
while(cin >> n){
// 木塊位置初始化
for(int i = 0; i<n; i++){
v[i].clear();
v[i].push_back(i);
}
// 執行搬運命令
while(cin >> s1 && s1 != "quit"){
cin >> a >> s2 >> b;
find_block(a, pa, ha);
find_block(b, pb, hb);
if(pa != pb){
if(s1 == "move") clear_above(pa, ha);
if(s2 == "onto") clear_above(pb, hb);
pile_over(pa, ha, pb);
}
}
show(); // 顯示結果
}
return 0;
}
// 找木塊位置
void find_block(int b, int &p, int &h){
for(p = 0; p<n; p++){
for(h = 0; h<v[p].size(); h++){
if(v[p][h] == b) return;
}
}
}
void clear_above(int p, int h){
for(int i = v[p].size()-1; i>h; i--){
int b = v[p][i];
v[b].push_back(b); // 木塊歸位
v[p].pop_back();
}
}
// 移動含木塊及其以上所有木塊
void pile_over(int p1, int h, int p2){
v[p2].insert(v[p2].end(), v[p1].begin() + h, v[p1].end());
v[p1].erase(v[p1].begin() + h, v[p1].end());
return;
}
// 顯示所有木塊
void show(){
for(int i = 0; i<n; i++){
cout << i << ":";
for(int j = 0; j<v[i].size(); j++){
cout << " " << v[i][j];
}
cout << "\n";
}
}
```
### e155
1 2 3 4 5 6 7
3 4 5 6 7 2 丟掉 1
5 6 7 2 4 丟掉 3
7 2 4 6 丟掉 5
4 6 2 丟掉 7
2 6 丟掉 4
6 丟掉 2
```cpp=
#include<bits/stdc++.h>
using namespace std;
int main(){
int n;
while(cin >> n && n){
queue<int> q;
for(int i = 1; i<=n; i++) q.push(i);
bool first = true;
cout << "Discarded cards: ";
while(q.size() > 1){
// 丟卡
if(first) first = false;
else cout << ", ";
cout << q.front(); q.pop();
// 把下一張卡放到最後
q.push(q.front()); q.pop();
}
cout << "\n";
cout << "Remaining card: " << q.front() << "\n";
}
}
```
### f166
input:
5 3 1 2
0 2 4 3 1
S = {0,2,4,3,1}
起始點為 s[0]= 0 ,首先先判斷向左(會出界),再來判斷向右(走到s[2]),接著傳送至 s[s[2]] = s[4] 的位置,因為不會連續傳送所以在 s[4] = 1 的位置選擇向左或向右,向右會出界,所以向左(走到s[3] = 3的位置),故可知 P = 3(終點等於3) 計算花費步數該題結束。
```cpp=
#include<bits/stdc++.h>
using namespace std;
int S[1000000]; // 最大可站立次數
int dis[1000000]; // 顯示第 x 步 站立的格子
int main(){
// n:場地格數; P:目的地; L & R: 往左/右移動的格數
int n, P, L, R;
// s: 角色當前所在格子編號; sL & sR 角色往左/右移動後的格子編號
int s, sL, sR;
// q: 紀錄未走過格子的佇列變數;
queue<int> q;
// 輸入 n P L R 以及每個格子傳送到的格子編號
cin >> n >> P >> L >> R;
for(int i = 0; i<n; i++) cin >> S[i];
q.push(0);
while(1){
// 結束迴圈條件1 : 無法到達終點 P
if(q.empty()){
cout << -1;
break;
}
// 結束迴圈條件2 : 到達終點 P
s = q.front(); q.pop(); // 取得當下位置
if(s == P){
cout << dis[s];
break;
}
// 往左
sL = s - L;
if(sL >= 0){
sL = S[sL]; // 處理傳送
// 傳送後的位置 是否出界 且 該格子是否還沒到達過
if(0 <= sL && sL < n && dis[sL] == 0){
dis[sL] = dis[s] + 1;
q.push(sL);
}
}
// 往右
sR = s + R;
if(sR <= n){
sR = S[sR]; // 處理傳送
// 傳送後的位置 是否出界 且 該格子是否還沒到達過
if(0 <= sR && sR < n && dis[sR] == 0){
dis[sR] = dis[s] + 1;
q.push(sR);
}
}
}
}
```
### b838
```cpp=
#include<bits/stdc++.h>
using namespace std;
int main(){
int t;
cin >> t;
while(t--){
// 讀取括號 & 準備前置變數
string s;
cin >> s;
stack<char> up; // 紀錄上括號出現的次數
bool vaild = true; // 檢驗括號組是否正確
int ans = 0; // 有效括號組的數量
// 處理括號問題
for(auto c : s){
if(c == '(') up.push('(');
else {
// 檢查上括號up 變數是否還有上括號
if(!up.empty() && up.top() == '('){
ans++;
up.pop();
} // ) 太多的情況
else { // up 變數用完的情況,代表下括號太多
vaild = false;
break;
}
}
}
if(!up.empty()) vaild = false;
cout << (vaild ? ans : 0) << "\n";
}
}
```
### h028
input:
6 140
10 30 50 70 100 125
30 15 55 10 55 25
output:
4
30
解題思路: 先將林場左右兩端放置無限高的"假樹",判斷樹往左往右砍的情況,判斷完之後回到 `stack s` 沒有被砍的樹是否可以被往左往右砍 `s.top().first + s.top().second` 未砍倒的樹的座標加上樹高。
```cpp=
#include<iostream>
#include<algorithm>
#include<climits>
#include<stack>
using namespace std;
// 最大樹的數量 first 存樹的座標, second 存樹的高度
pair<int, int> T[100002];
// 樹坐大只有100000,左右邊界各加1
int main(){
int N, L;
int total = 0, height = 0; // total 砍倒樹的數量, 砍倒樹最高的高度
stack<pair<int, int>> s; // 紀錄最左側砍倒的樹木
cin >> N >> L;
// 繪製林場
T[0].first = 0; // 最左邊的邊界
T[0].second = INT_MAX; // int 的最大值
for(int i = 1; i<=N; i++) cin >> T[i].first; // 讀樹的座標
for(int i = 1; i<=N; i++) cin >> T[i].second; // 讀樹的高度
T[N+1].first = L;
s.push(T[0]); //
for(int i = 1; i<=N; i++){
// 檢查樹木往左砍的狀況、往右砍的情況
if(T[i].first-T[i].second >= s.top().first || T[i].first+T[i].second <= T[i+1].first){
total++;
height = max(height, T[i].second);
// 檢查左側未砍倒樹木是否可以砍倒
while(s.top().first + s.top().second <= T[i+1].first){
total++;
height = max(height, s.top().second);
s.pop();
}
}
else {
s.push(T[i]); // 加上未能砍倒的樹木
}
}
cout << total << "\n" << height;
}
```
### d217
運用容器 `<set>` 裡面的函式 `aa.count(x)` 來判斷是否前面以猜過相同的字元
解題步驟: 輸入 -> for遍歷答案的每個字元 -> if判斷是否猜過此字元 -> 判斷有無猜到題目字元 -> 是否達成結束條件(勝利或敗北)
```cpp=
#include<iostream>
#include<string>
#include<set>
#define ALL(x) x.begin(), x.end()
using namespace std;
int main(){
int round;
while(cin >> round && round != -1){
// 讀題目與猜測和是這前置變數
string q, a;
int lc = 7; // 失敗條件
cin >> q >> a;
// 前處理
printf("Round %d\n", round);
set<char> uc(ALL(q)); // 刪除題目重複的英文字元
set<char> aa; // 儲存以猜過的字元
for(int i = 0; i<a.size(); i++){
// 確認玩家猜測字元是否已經猜過了
if(aa.count(a[i])) continue; // 猜過: 跳過本次迴圈
aa.insert(a[i]); // 未猜過: 加入猜過自原變數
// 判斷有無猜到題目字元
if(uc.count(a[i])) uc.erase(a[i]);
else lc--;
// 是否達成結束條件(勝利或敗北)
if(uc.empty() || !lc) break;
}
if(uc.empty()) cout << "You win.\n";
else if(lc == 0) cout << "You lose.\n";
else cout << "You chickened out.\n";
}
return 0;
}
```
### f698
1 2 3 * +
當遇到 `*` 的時候 3x2,接著遇到 `+` 的時候 6 + 1,答案為7。
```cpp=
#include<iostream>
#include<string>
#include<stack>
using namespace std;
int main(){
string ops = "+-*/"; // 運算符號
stack<int> num;
string s;
while(cin >> s){ // 1 2 3 * +
int op = ops.find(s); // 從 ops 找 s 字串
if(op == -1) // 讀取值為數字
num.push(stoi(s)); // string 轉成int儲存
else {
int n2 = num.top(); num.pop();
int n1 = num.top(); num.pop();
switch (op)
{
case 0:
num.push(n1 + n2); break;
case 1:
num.push(n1 - n2); break;
case 2:
num.push(n1 * n2); break;
default:
num.push(n1 / n2);
}
}
}
cout << num.top();
return 0;
}
```
### d244
```cpp=
#include<iostream>
#include<map>
using namespace std;
int main(){
// 前置宣告
// st.first: 石頭編號; st.second: 對應編號個數
map<int, int> st;
int n;
// 9 8 6 9 8 2 3 5 2 1 6 8 1 5 1 2 3 3 5 9
// 1 : 3
// 2 : 3
// 3 : 3
// 4 : 3
// 5 : 3
// 6 : 2
// 7 : 3
// 8 : 3
// 9 : 3
while(cin >> n) st[n]++;
// 找尋不足3顆石頭編號
for(auto p : st){
if(p.second % 3){
cout << p.first;
break;
}
}
return 0;
}
```
### e564
團隊編號先將被刪除的團隊開始依序刪除
> 例如 101 102 103、201 202 203 團隊
> 若 ENQUEUE 101 、 ENQUEUE 201 則 會先將 `10 團隊` 裡面的所有值印出
> 故輸出為: 101 102 103 201 202 203
> 利用 map 建表 team[x] 為團隊編號,當輸入 ENQUEUE 檢查該成員是否為團隊編號首位進入佇列的成員,如果是就要先放到**輸出佇列**(要先把輸出佇列裡面的資料印出)
```cpp=
#include<iostream>
#include<queue>
#include<map>
using namespace std;
int main(){
// 前置宣告
int t, n, x; // t: 團隊數量; n: 團隊成員數; x: 成員編號
int t0; // 處理當下成員的團隊數量
int kase = 0; // 印出結果 Scenario # 的編號
// 開始處理團體佇列
while(cin >> t && t){
// team.first 團隊成員; team.second 成員所屬團隊編號
map<int, int> team;
// 給予所有成員所屬的團隊編號
for(int i = 0; i<t; i++){
cin >> n;
while(n--){
cin >> x;
team[x] = i;
}
}
// ENQUEUE & DEQUEUE 處理
queue<int> tqueue; // 正在佇列中的團隊編號
queue<int> equeue[1000]; // 正在佇列中的成員
printf("Scenario #%d\n", ++kase); // 印出 Scenario #
string cmd;
while(cin >> cmd && cmd != "STOP"){
if(cmd == "ENQUEUE"){
cin >> x;
t0 = team[x]; // 取得成員 x 的所屬團隊編號
if(equeue[t0].empty()) // 檢查該成員是否為團隊編號首位進入佇列的成員
tqueue.push(t0); // 團隊編號加入佇列
equeue[t0].push(x); // 成員加入佇列
}
else { // cmd == "DEQUEUE"
t0 = tqueue.front(); // 取得離開成員所屬的團隊編號
cout << equeue[t0].front() << "\n"; // 印出要離開佇列的成員
equeue[t0].pop(); // 將成員刪除
if(equeue[t0].empty()){ // 如果該團隊都被輸出出來後,則該團隊離開 輸出佇列
tqueue.pop();
}
}
}
cout << "\n";
}
}
```
### e548 I Can Guess the Data Structure!
```cpp=
#include<iostream>
#include<stack>
#include<queue>
using namespace std;
int main() {
//讀取命令數
int n;
while (cin >> n) {
//準備前置變數
//ans[0]: 堆疊; ans[1]: 佇列; ans[2]: 優先佇列
bool ans[] = { true, true, true };
stack<int> s;
queue<int> q;
priority_queue<int> pq;
//開始檢查資料結構的型態
while (n--) {
int cmd, x;
cin >> cmd >> x;
if (cmd == 1) {
s.push(x);
q.push(x);
pq.push(x);
}
else { //cmd == 2
if (ans[0]) { //堆疊結構檢查
if (!s.empty() && x == s.top()) s.pop();
else ans[0] = false;
}
if (ans[1]) { //佇列結構檢查
if (!q.empty() && x == q.front()) q.pop();
else ans[1] = false;
}
if (ans[2]) { //優先佇列檢查
if (!pq.empty() && x == pq.top()) pq.pop();
else ans[2] = false;
}
}
}
//根據 ans 變數輸出結果
if (ans[0] && !ans[1] && !ans[2]) cout << "stack";
else if (!ans[0] && ans[1] && !ans[2]) cout << "queue";
else if (!ans[0] && !ans[1] && ans[2]) cout << "priority queue";
else if (!ans[0] && !ans[1] && !ans[2]) cout << "impossible";
else cout << "not sure";
cout << "\n";
}
}
```
### d221
input:
3
1 2 3
4
1 2 3 4
0
使用 prioritty_queue 的降冪排列改成升冪排列這樣的話放進去的資料會從最小的資料為 `pq.top()` 若從最小的資料開始算,則 cost 會是最小
```cpp=
#include<iostream>
#include<queue>
using namespace std;
int main(){
int N;
while(cin >> N && N){
priority_queue<int, vector<int>, greater<int>> pq;
for(int i = 0; i<N; i++){
int x;
cin >> x;
pq.push(x);
}
long long sum, cost = 0;
while(pq.size() != 1){
sum = pq.top(); pq.pop();
sum = sum + pq.top(); pq.pop();
pq.push(sum);
cost = cost + sum;
}
cout << cost << "\n";
}
}
```
### h083
先把籤的字串分成[A][B][C], 且要使 [A] == [C], 則找出 [B] 字串,接著利用 binary_search 找[B]字串等於其他籤的字串。
```cpp=
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
#define ALL(x) x.begin(), x.end()
int main(){
// 製作籤筒並且將籤放入
vector<string> v;
int m;
cin >> m;
while(m--){
string s;
cin >> s;
v.push_back(s); // 把籤放入籤筒
}
sort(ALL(v));
int ans = 0; // 紀錄聖筊數量
for(auto s : v){
// 規則1: 把籤的字串分成[A][B][C], [A] == [C]
int len = s.size(); // 當前籤的長度
for(int i = 1; i<=len/2; i++){
string sa = s.substr(0, i); // [A] 字串
string sc = s.substr((len - i), i); // [C] 字串
if(sa == sc){
string sb = s.substr(i, len - 2 * i); // [B] 字串
// 規則2: [B]字串等於其他籤的字串
if(binary_search(ALL(v), sb)){
ans++;
}
}
}
}
// Sample :
// 5
// abyyyab
// y
// yy
// yyy
// yyyy
// 組合一: abyyyab、yyy
// 組合二: yyy、y
// 組合三: yyyy、yy
cout << ans;
return 0;
}
```
### h640
```cpp=
#include<bits/stdc++.h>
using namespace std;
int func(void);
int main(void){
cout << func() << "\n";
return 0;
}
int func(void){
string token;
int x, y, z; // 讀取 token
cin >> token;
// token == "f"
if(token == "f"){
x = func();
return 2*x - 3;
}
// token == "g"
if(token == "g"){
x = func();
y = func();
return 2*x + y - 7;
}
if(token == "h"){
x = func();
y = func();
z = func();
return 3*x - 2*y + z;
}
return(stoi(token));
}
```
### f637
```cpp=
#include<iostream>
#include<string>
using namespace std;
int decode(int n); // 計算弧形的邊長
string s; // 讀取編碼的邊數
int p = -1; // 編碼的指標 index
int main(){
int n; // 圖像邊長
cin >> s >> n; // 讀取編碼
cout << decode(n) << "\n";
return 0;
} // input: 2200101020110
int decode(int n){
int black = 0; // 黑色像素的數量
p++;
if(s[p] == '0') // 0: 白色像素
black = 0;
else if(s[p] == '1') // 1: 黑色像素
black += n*n;
else { // 把影像分四等份
for(int i=0; i<4; i++){
black = black + decode(n/2);
}
}
return black;
}
```
### k733
```cpp=
#include<iostream>
#include<string>
using namespace std;
long long loop(int& head, int& tail); // 遞迴副程式
string s0; // 磁軌的指令
int tape = 0; // 磁軌命令的指標
int main(){
int h, t; // h: head & t = tail
long long walk = 0; // 紀錄總移動單位
cin >> s0;
walk = loop(h, t); // 移動距離單位
walk += abs(h-10); // 扣除 Txx 最小值從10開始
cout << walk;
return 0;
} // input: T10L2T15T22L2T15ET23ET44
long long loop(int& head, int& tail){
// 副程式的資料初始化
// walk: 移動距離, subwalk: 迴圈內的移動距離
long long walk = 0, subwalk;
int num, h, t; // num: 讀取 x 數字的變數
head = tail = -1; // 給予 head & tail 初始值 -1
while(1){
// 結束點1: 整個磁軌命令執行完
if(tape == s0.size()) return walk;
// 命令整理
if(s0[tape] == 'T'){ // 讀取命令 T
num = stoi(s0.substr(tape+1, 2)); // 取得位置
tape = tape + 3; // 移動磁軌命的指標 Txx 跳3格
if(tail == -1) // 初次讀取命令的判定
head = num;
else
walk = walk + abs(num-tail);
tail = num;
}
else if(s0[tape] == 'L'){ // 讀取命令 L
num = stoi(s0.substr(tape+1, 1));
tape = tape + 2;
subwalk = loop(h, t); // 透過遞迴處理 Lx...E 的迴圈
if(tail == -1)
head = h; // 把位置給 head
else
walk = walk + abs(h-tail);
tail = t;
walk = walk + subwalk * num + abs(h-t) * (num - 1);
}
else { // 讀取命令 E
tape++; // 移動磁軌命令的指標
return walk; // 結束點2: 回傳回圈內移動距離
}
}
}
```