题解 | #简单密码#

简单密码

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;
}
华为题库题解 文章被收录于专栏

牛客华为题库的题解

全部评论

相关推荐

06-10 23:36
已编辑
首都经济贸易大学 C++
点赞 评论 收藏
分享
我是没经验的毕业生,这啥情况啊会不会是hr在刷kpi
JamesGosli...:字节boss属于是群发了,我都快入职字节了,其他部门还在和我boss打招呼
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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