题解 | #字符串加解密#

字符串加解密

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

全部评论

相关推荐

昨天 11:50
已编辑
江苏科技大学 后端
feedmob Ruby开发(Ruby on Rails框架) 12k base
点赞 评论 收藏
分享
03-01 23:20
门头沟学院 Java
野猪不是猪🐗:美团4000hc就离谱,这是把26实习和25春招的直接加一起了?
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务