题解 | #字符串加解密#

字符串加解密

https://www.nowcoder.com/practice/2aa32b378a024755a3f251e75cbf233a

// HJ29 字符串加解密.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
#include<iostream>
#include<bits/stdc++.h>

using namespace std;

char dp1[] = {"bcdefghijklmnopqrstuvwxyzaBCDEFGHIJKLMNOPQRSTUVWXYZA1234567890"};
char dp2[] = {"ZABCDEFGHIJKLMNOPQRSTUVWXYzabcdefghijklmnopqrstuvwxy9012345678"};

class Solution {
public:
	void Encryption(string& s);
	void Decode(string& s);
};

void Solution::Encryption(string& s)
{
	if (s.empty())
		return;
	for (int i = 0; i < s.size(); i++)
	{
		if (isupper(s[i]))
		{
			s[i] = dp1[s[i] - 'A'];
		}
		else if (islower(s[i]))
		{
			s[i] = dp1[s[i] - 'a' + 26];
		}
		else if (isdigit(s[i]))
		{
			s[i] = dp1[s[i] - '0' + 52];
		}
	}
	cout << s << endl;
}

void Solution::Decode(string& s)
{
	if (s.empty())
		return;
	for (int i = 0; i < s.size(); i++)
	{
		if (islower(s[i]))
		{
			s[i] = dp2[s[i] - 'a'];
		}
		else if (isupper(s[i]))
		{
			s[i] = dp2[s[i] - 'A' + 26];
		}
		else if (isdigit(s[i]))
		{
			s[i] = dp2[s[i] - '0' + 52];
		}
	}
	cout << s << endl;
}

int main()
{
	Solution a, b;
	string str1, str2;
	while (cin >> str1 >> str2)
	{
		a.Encryption(str1);
		b.Decode(str2);
	}
	return 0;
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

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