题解 | #简单密码#
简单密码
https://www.nowcoder.com/practice/ff99c43dd07f4e95a8f2f5448da3772a
#include<bits/stdc++.h> using namespace std; //该函数用于将密文翻译为明文 vector<char> translate(string s) { vector<char> result; unordered_map<char, char> letter = {//记录密文与明文的对应关系 {'A', 'V'}, {'B', 'W'}, {'C', 'X'}, {'D', 'Y'}, {'E', 'Z'}, {'F', 'A'}, {'G', 'B'}, {'H', 'C'}, {'I', 'D'}, {'J', 'E'}, {'K', 'F'}, {'L', 'G'}, {'M', 'H'}, {'N', 'I'}, {'O', 'J'}, {'P', 'K'}, {'Q', 'L'}, {'R', 'M'}, {'S', 'N'}, {'T', 'O'}, {'U', 'P'}, {'V', 'Q'}, {'W', 'R'}, {'X', 'S'}, {'Y', 'T'}, {'Z', 'U'} }; for (char c : s) {//遍历密文字符串 if (isalpha( c)) {//如果该字符是字母,则通过对应关系转换为明文 result.push_back(letter[c]); } else {//如果该字符不是字母,则照常输出 result.push_back(c); } } return result; } int main() { string str; vector<char> result; while (getline(cin, str)) { if (str == "ENDOFINPUT") { break; } if (str != "START" && str != "END") { result = translate(str);//翻译密文 for (auto iter = result.begin(); iter != result.end(); iter++) {//将转换的结果输出 cout << *iter; } cout << endl; } } }