题解 | #简单密码#
简单密码
http://www.nowcoder.com/practice/7960b5038a2142a18e27e4c733855dac
来个map映射一下ok了,z的ascII码122,后面一个是'{',Z是90。注意string的find找不到返回string::npos而不是0判断条件时要注意
#include <string>
#include <map>
#include <vector>
using namespace std;
int main() {
map<string,string> m ={
{"abc","2"},
{"def","3"},
{"ghi","4"},
{"jkl","5"},
{"mno","6"},
{"pqrs","7"},
{"tuv","8"},
{"wxyz","9"},
};
string str,temp;
while(cin>>str){
for(int i = 0;i<str.size();i++){
if(islower(str[i])){
for(auto &it:m){
if((it.first.find(str[i]))!=string::npos) temp+=it.second;
}
}
else if (isupper(str[i])){
str[i] = tolower(str[i]);
if((str[i]+1)!=123) temp+=(str[i]+1);
else temp+='a';
}
else
temp+=str[i];
}
cout<<temp<<endl;
}
}