HJ21 简单密码(c++)

题目链接:简单密码_牛客题霸_牛客网

注意点:

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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值