题解 | #字符串加解密#

字符串加解密

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

全部评论

相关推荐

小鹏、大疆、米哈游、MinMax小鹏上午投的下午就约面,进度未免也太快了
蛇年行大运fff:哥们 盗贴有意思吗,我发xhs上的给你搬过来了😅😅😅
点赞 评论 收藏
分享
06-12 17:46
门头沟学院 Java
运营你豪哥:来说重点: ​1.项目前置,时间倒序。​​ 2.​项目描述强化结果与量化效果(STAR原则里的R)。​​ ​3.个人技能精炼,明确掌握程度,突出核心。​​ ​4.增加强有力开头的个人总结部分。​​ 5.​优化教育背景(成绩排名)、合并奖项与活动。​​
听劝,我这个简历该怎么改...
点赞 评论 收藏
分享
机械打工仔:有说的你怀疑一下就行了,直接问也太实诚了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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