题解 | #简单密码#
简单密码
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;
}
华为题库题解 文章被收录于专栏
牛客华为题库的题解