<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);"> 字符串的加密与解密,加密的方式是将字符串中每个字符加上它在字符串中的位置和一个偏移值5。如"mrsoft",第一个字符"m"在字符串中的位置为0,那么它对应的密文是“‘m’+0+5”,即r。</span>
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">
</span>
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">
</span>
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">
</span>
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">
</span>
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">
</span>
#include<iostream>
#include<string>
int main()
{
using namespace std;
string s;
getline(cin, s);
for(int i = 0; i < s.size(); i++)
{
int temp = (i + 5) % 26;//判断循环了多少轮余多少次
if((s[i] + temp <= 'z' && s[i] >= 'a') || (s[i] + temp <= 'Z' && s[i] >= 'A'))
{
s[i] = (char)(s[i] + temp);
}
else
{
s[i] = (char)(s[i] - 26 + temp);
}
}
cout << s << endl;
for(int i = 0; i < s.size(); i++)
{
int temp = (i + 5) % 26;
if((s[i] - temp >= 'a' && s[i] <= 'z') || (s[i] - temp >= 'A' && s[i] <= 'Z'))
s[i] = (char)(s[i] - temp);
else
s[i] = (char)(s[i] + 26 - temp);
}
cout << s << endl;
return 0;
}