题解 | #字符串加解密#

字符串加解密

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")

全部评论

相关推荐

09-22 15:45
门头沟学院 Java
谁给娃offer我给...:我也遇到了,我说只要我通过面试我就去,实际上我根本就不会去😁
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务