题解 | #字符串加解密#
字符串加解密
https://www.nowcoder.com/practice/2aa32b378a024755a3f251e75cbf233a
#include <iostream> #include <string> using namespace std; // 依据规则对改变每个字符 int main() { string str1, str2; getline(cin, str1); getline(cin, str2); for (int i = 0; i < str1.size(); ++i) { if (str1[i] <= 'z' && str1[i] >= 'a') { if (str1[i] == 'z') str1[i] = 'A'; else str1[i] -= 31; } else if (str1[i] <= 'Z' && str1[i] >= 'A') { if (str1[i] == 'Z') str1[i] = 'a'; else str1[i] += 33; } else { if (str1[i] == '9') str1[i] = '0'; else str1[i]++; } } for (int i = 0; i < str2.size(); ++i) { if (str2[i] <= 'z' && str2[i] >= 'a') { if (str2[i] == 'a') str2[i] = 'Z'; else str2[i] -= 33; } else if (str2[i] <= 'Z' && str2[i] >= 'A') { if (str2[i] == 'A') str2[i] = 'z'; else str2[i] += 31; } else { if (str2[i] == '0') str2[i] = '9'; else str2[i]--; } } cout << str1<<endl; cout << str2<<endl; } // 64 位输出请用 printf("%lld")
#include <iostream> #include <string> using namespace std; // 字典法 // 列出解密/加密符号的对应值 int main() { string a = "zabcdefghijklmnopqrstuvwxyZABCDEFGHIJKLMNOPQRSTUVWXY9012345678"; string b = "bcdefghijklmnopqrstuvwxyzaBCDEFGHIJKLMNOPQRSTUVWXYZA1234567890"; string str1, str2; getline(cin, str1); getline(cin, str2); for (int i = 0; i < str1.size(); ++i) { char ch = str1[i]; if (ch >= 'A' && ch <= 'Z') str1[i] = b[ch - 'A']; else if (ch >= 'a' && ch <= 'z') str1[i] = b[ch - 'a' + 26]; else str1[i] = b[ch - '0' + 52]; } for (int j = 0; j < str2.size(); ++j) { char ch = str2[j]; if (ch >= 'A' && ch <= 'Z') str2[j] = a[ch - 'A']; else if (ch >= 'a' && ch <= 'z') str2[j] = a[ch - 'a' + 26]; else str2[j] = a[ch - '0' + 52]; } cout << str1 << endl; cout << str2 << endl; } // 64 位输出请用 printf("%lld")