题解 | #简单密码#

简单密码

http://www.nowcoder.com/practice/7960b5038a2142a18e27e4c733855dac

//#include <bits/stdc++.h>
#include <iostream>
#include <string>
#include <unordered_map>
#include <cstring>

using namespace std;

unordered_map<string, char> m = {
    {"1", '1'},
    {"abc", '2'},
    {"def", '3'},
    {"ghi", '4'},
    {"jkl", '5'},
    {"mno", '6'},
    {"pqrs", '7'},
    {"tuv", '8'},
    {"wxyz", '9'},
    {"0", '0'},
};


int main(){
    string str = "";
    cin>>str;

    string res = "";
    for(char c : str){
        char ch;
        if(!isdigit(c)){ //如果不是数字
            if(c >= 'a' && c <= 'z'){
                for(auto iter = m.begin(); iter != m.end(); iter++){  
                    //cout<<iter->first.c_str()<<endl;
                    //cout<<iter->second<<endl;
                    if(iter->first.find(c) != iter->first.npos){ //找到了
                        ch = iter->second;
                        //cout<<ch<<endl;
                        res += ch;
                        break;
                    }
                }              
            }
            
            else if(c >= 'A' && c <= 'Z'){
                if(c == 'Z'){ //特殊情况
                    ch = 'a';
                }
                else{
                    ch = c + 33; //先变成小写,再往后移一位
                }            
                res += ch;
            }
            
        }
        else{
            res += c;
        }
        
    }
    
    cout<<res<<endl;
    
    return 0;
}
华为题库题解 文章被收录于专栏

牛客华为题库的题解

全部评论

相关推荐

昨天 10:41
已编辑
门头沟学院 C++
面试官说,他面到现在只有一个人拿了纸质简历,悲惨的是我也没拿
找到工作就狠狠玩CSGO:没要求带简历,但确实最好带,我带了之后面试官直接对着纸质简历问的,最后我还特意把简历留下来了,希望能加深一下印象
点赞 评论 收藏
分享
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务