题目链接:简单密码_牛客题霸_牛客网
注意点:
1.switch语句
2.考虑哈希表的实现,推荐好:c++ map与unordered_map区别及使用_别说话写代码的博客-CSDN博客_unordered_map
3.小写字母转数字,推荐采用unordered_map 来存放映射关系;
大写字母转小写,可以直接加32
4.isalpha()函数:C 库函数 – isalpha() | 菜鸟教程
方法一:
#include <iostream>
using namespace std;
int main()
{
string str;
while (cin >> str)
{
for(int i = 0; i < str.size(); ++i )
{
switch (str[i]) {
case 'A'...'Y' : str[i] += 33; break;
case 'Z' : str[i] = 'a' ;break;
case 'a' ... 'c': str[i] = '2'; break;
case 'd' ... 'f': str[i] = '3'; break;
case 'g' ... 'i': str[i] = '4'; break;
case 'j' ... 'l': str[i] = '5'; break;
case 'm' ... 'o': str[i] = '6'; break;
case 'p' ... 's': str[i] = '7'; break;
case 't' ... 'v': str[i] = '8'; break;
case 'w' ... 'z': str[i] = '9'; break;
default: break;
}
}
cout << str <<endl;
}
return 0;
}
方法二:
#include <iostream>
#include <unordered_map>
#include <ctype.h>
using namespace std;
int main()
{
unordered_map<char,char> m;
m['a'] = m['b'] =m['c'] ='2';
m['d'] = m['e'] = m['f'] ='3';
m['g'] = m['h'] = m['i'] ='4';
m['j'] = m['k'] = m['l'] ='5';
m['m'] = m['n'] = m['o'] ='6';
m['p'] = m['q'] = m['r'] = m['s'] ='7';
m['t'] = m['u'] = m['v'] ='8';
m['w'] = m['x'] = m['y'] = m['z'] = '9';
m['Z'] = 'a';
string str;
while (cin >> str)
{
for(int i = 0; i < str.size(); ++i )
{
//两个条件的if不可以反
if(str[i] >= 'A' && str[i] < 'Z')
{
str[i] = str[i] - 'A' + 'a' + 1;
}
else if (isalpha(str[i]))
{
str[i] = m[str[i]];
}
}
cout << str <<endl;
}
return 0;
}
|