大水题:
给一个字符串加密的方法:老式手机键盘应该按的次数 + 当前是第几个字符,老式手机键盘按那么多次后得到的字母即加密后的字母。
如对ABCD加密,A应该是1按1次,但是是第1个字符,所以加密成A按2次的样子,变成B,依次加密为BACE。
现在给出加密后的字符串求加密前的样子。注意大小写
#include <iostream>
using namespace std;
string s;
string data[8] = {"ABC","DEF","GHI","JKL","MNO","PQRS","TUV","WXYZ"};
int index(char c)
{
for (int i = 7; i >= 0; -- i)
if (toupper(c) >= data[i][0]) return i;
return -1;
}
int ind, cod;
int main()
{
while (true)
{
cin >> s;
if (s[0] == '#') break;
for (int i = s.length()-1; i >= 0; -- i)
{
ind = index(s[i]);
cod = (toupper(s[i])-data[ind][0]-i-1+40*data[ind].length()) % data[ind].length();
if (toupper(s[i]) == s[i]) s[i] = data[ind][cod];
else s[i] = tolower(data[ind][cod]);
}
cout << s << endl;
}
}
本文介绍了一种基于老式手机键盘的字符串加密方法,并提供了解密算法的实现代码。该算法考虑了加密过程中每个字符的位置及按键次数,通过逆向过程还原原始字符串。
411

被折叠的 条评论
为什么被折叠?



