题解 | #字符串加解密#
字符串加解密
http://www.nowcoder.com/practice/2aa32b378a024755a3f251e75cbf233a
#include <string>
using namespace std;
void myfunc(string code){
string tempstr;
int distance;
//小写大,大写小
distance = 'a' - 'A';
for (int i = 0; i < code.size(); ++i) {
if (code[i]>'a'-1&&code[i]<'z'+1){
//小写字母
if (code[i] == 'z'){
tempstr.push_back('A');
} else{
int a = code[i] + 0;
tempstr.push_back(a-distance+1);
}
} else if (code[i]>'A'-1&&code[i]<'Z'+1){
//大写字母
if (code[i] == 'Z'){
tempstr.push_back('a');
} else{
int a = code[i] + 0;
tempstr.push_back(a+distance+1);
}
} else if (code[i]>'0'-1&&code[i]<'9'+1){
//数字
if (code[i] == '9'){
tempstr.push_back('0');
} else{
int temppp = code[i] - '0';
tempstr.push_back('0'+1+temppp);
}
} else{
//其他不变
tempstr.push_back(code[i]);
}
}
cout << tempstr << endl;
}
void myversefunc(string cipher){
string tempstr;
int distance;
//小写大,大写小
distance = 'a' - 'A';
for (int i = 0; i < cipher.size(); ++i) {
if (cipher[i]>'a'-1&&cipher[i]<'z'+1){
//小写字母
if (cipher[i] == 'a'){
tempstr.push_back('Z');
} else{
int a = cipher[i] + 0;
tempstr.push_back(a-distance-1);
}
} else if (cipher[i]>'A'-1&&cipher[i]<'Z'+1){
//大写字母
if (cipher[i] == 'A'){
tempstr.push_back('z');
} else{
int a = cipher[i] + 0;
tempstr.push_back(a+distance-1);
}
} else if (cipher[i]>'0'-1&&cipher[i]<'9'+1){
//数字
if (cipher[i] == '0'){
tempstr.push_back('9');
} else{
int temppp = cipher[i] - '0';
tempstr.push_back('0'-1+temppp);
}
} else{
//其他不变
tempstr.push_back(cipher[i]);
}
}
cout << tempstr << endl;
}
int main() {
string code;
string cipher;
getline(cin,code);
getline(cin,cipher);
myfunc(code);
myversefunc(cipher);
return 0;
}
//解密编写完毕,明天编写逆解密
ASCII码好好用就行,一个处理字符的没有技巧的题

查看2道真题和解析