题解 | #字符串加解密#
字符串加解密
https://www.nowcoder.com/practice/2aa32b378a024755a3f251e75cbf233a
//这道题难的是构造上
#include <iostream>
#include <string>
using namespace std;
string encrypt(const string s){
string res="";
if(!s.empty()){
for(auto ch:s){
//字母
char c;
if(isupper(ch)&&ch!='Z')
c = ::tolower(ch+1);
else if(ch == 'Z')
c = 'a';
else if(islower(ch)&&ch!='z')
c = ::toupper(ch+1);
else if(ch == 'z')
c = 'A';
//数字
else if(isdigit(ch)&&ch!='9')
c = ch + 1;
else if(ch == '9')
c = '0';
else
continue;
res += c;
}
}
return res;
}
string decrypt(const string s){
string res;
if(!s.empty()){
for(auto ch:s){
//字母
char c;
if(isupper(ch)&&ch!='A')
c = ::tolower(ch-1);
else if(ch == 'A')
c = 'z';
else if(islower(ch)&&ch!='a')
c = ::toupper(ch-1);
else if(ch == 'a')
c = 'Z';
//数字
else if(isdigit(ch)&&ch!='0')
c = ch - 1;
else if(ch == '0')
c = '9';
else
continue;
res += c;
}
}
return res;
}
int main() {
string en_str, de_str;
cin >> en_str >> de_str;
cout << encrypt(en_str)<<endl;
cout << decrypt(de_str) << endl;
return 0;
}
// 64 位输出请用 printf("%lld")
查看20道真题和解析