# 解題紀錄 ## 題目:f327. 刪除欄位 ## 📙 題目描述 文文做了一個很大的試算表,過程中發現有一些連續的欄位不再需要了, 於是把它們刪除。但是他很好奇他刪掉了幾欄。給你所刪除的起始及結束的欄位名稱,請你幫他算算看一共有幾欄。 **範例:** ```txt 輸入說明 輸入只有一行,含有起始及結束的欄位名稱。欄位名稱由大寫字母構成,介於 A 和 FXSHRXW 之間 (含),兩個欄位名稱間以空白隔開。 輸出說明 輸出一個整數,代表文文刪除了幾欄。 ``` ```txt 範例輸入 #1 範例輸出 #1 A Z 26 ``` ```txt 範例輸入 #2 範例輸出 #2 AA ZZ 676 ``` --- ## ✒️ 解題思路 1. **解析式子** - 這題數字是由 `A` ~ `Z`組成 可以想成是`26`進位 - `A`為`1` `Z`為`26` 2. **字串實作** - 宣告變數 `res` 代表結果 - 每次執行迴圈都將`res` * 26 模擬進位 - 遍歷字串陣列 將遇到的字元扣掉 `'A'`才是正確數字 - 將**數字 + 1**賦值(assign)到res( + 1 是因為起始位置為1) --- ## 💻 C++ 語言解法 ```c++ #include <iostream> #include <cmath> using namespace std; class Solution { private: string start; string end; int to_radix26(string n){ int res {0}; for(int i {0}; i < n.size(); i++){ res * = 26; res = n[i] - 'A' + 1; } return res; } public: Solution(){ cin >> start >> end; } void ans(){ cout << abs(to_radix26(start) - to_radix26(end)) + 1 << '\n'; } }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); Solution sol; sol.ans(); return 0; } ```