# Decode the Mad man(UVA10222) :::warning UVA跟ZJ的測資範圍不一樣,UVA不包含數字,ZJ包含數字 ::: ## [程式繳交區](https://hackmd.io/@Renektonn/Hks_uFsDyl/edit) ## 題目 [點我](https://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=14&page=show_problem&problem=1163) ## 解題網站 [UVA](https://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=14&page=show_problem&problem=1163) [ZJ](https://zerojudge.tw/ShowProblem?problemid=e578) ## 演算法 ``` 1.定義兩字串,str1是解密字串,包含所有英文字母,str2是加密字串,包含所有英文字母在鍵盤上右兩個鍵的字(不一定為英文字母),加解密對應的索引要是一樣的 2.輸入一行字串s 3.掃過s,對於s的每個字元,若它是大寫字母,先轉為小寫字母,若它出現在str2中,則印出它索引所對應的str1,否則,印出s的原始字元 ``` ## 程式碼 ```cpp= #include <iostream> #include <string> using namespace std; int main() { // 定義解密字串和加密字串 string str1 = "qwertyuiopasdfghjklzxcvbnm"; // 解密字串 string str2 = "ertyuiop[]dfghjkl;'cvbnm,."; // 加密字串(鍵盤右移兩個) // 輸入一行字串 s string s; getline(cin, s); // 掃過 s 的每個字元並進行加解密 for (int i = 0; i < s.length(); ++i) { char c = s[i]; if ('A' <= c and c <= 'Z') { c += 32; } int pos = str2.find(c); // 在加密字串中查找字元的位置 if (pos != -1) { // 若字元在加密字串中,輸出對應解密字串的字元 cout << str1[pos]; } else { // 否則,輸出原始字元 cout << c; } } cout << endl; // 換行輸出結束結果 return 0; } ```