题解 | #字符串加解密#

字符串加解密

https://www.nowcoder.com/practice/2aa32b378a024755a3f251e75cbf233a

#include <iostream>
#include <vector>
#include <string>

using namespace std;

int main() {
    string s;
    getline(cin, s);  // 输入第一个字符串
    string s1;
    getline(cin, s1);  // 输入第二个字符串
    
    vector<char> str;

    // 对第一个字符串进行转换
    for (auto& ch : s) {
        if ('a' <= ch && ch <= 'z') {
            // 小写字母变为大写并移动
            if (ch == 'z') {
                str.push_back('A');
            } else {
                str.push_back(ch - 'a' + 'A' + 1);
            }
        } else if ('A' <= ch && ch <= 'Z') {
            // 大写字母变为小写并移动
            if (ch == 'Z') {
                str.push_back('a');
            } else {
                str.push_back(ch - 'A' + 'a' + 1);
            }
        } else if ('0' <= ch && ch <= '9') {
            // 数字字符加 1
            if (ch == '9') {
                str.push_back('0');
            } else {
                str.push_back(ch + 1);
            }
        } else {
            // 其他字符保持不变
            str.push_back(ch);
        }
    }

    // 输出第一个字符串转换后的结果
    for (int i = 0; i < str.size(); i++) {
        cout << str[i];
    }
    cout << endl;

    vector<char> astr;

    // 对第二个字符串进行逆向转换
    for (const char& ch : s1) {
        if ('a' <= ch && ch <= 'z') {
            // 小写字母变为大写并向前移动
            if (ch == 'a') {
                astr.push_back('Z');
            } else {
                astr.push_back(ch - 'a' + 'A' - 1);
            }
        } else if ('A' <= ch && ch <= 'Z') {
            // 大写字母变为小写并向前移动
            if (ch == 'A') {
                astr.push_back('z');
            } else {
                astr.push_back(ch - 'A' + 'a' - 1);
            }
        } else if ('0' <= ch && ch <= '9') {
            // 数字字符减 1
            if (ch == '0') {
                astr.push_back('9');
            } else {
                astr.push_back(ch - 1);
            }
        } else {
            // 其他字符保持不变
            astr.push_back(ch);
        }
    }

    // 输出第二个字符串转换后的结果
    for (int i = 0; i < astr.size(); i++) {
        cout << astr[i];
    }
    cout << endl;

    return 0;
}

// 64 位输出请用 printf("%lld")

全部评论

相关推荐

07-02 13:52
门头沟学院 Java
点赞 评论 收藏
分享
头顶尖尖的程序员:我也是面了三四次才放平心态的。准备好自我介绍,不一定要背熟,可以记事本写下来读。全程控制语速,所有问题都先思考几秒,不要急着答,不要打断面试官说话。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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