原题地址:http://acm.jlu.edu.cn/joj/showproblem.php?pid=1064
很简单的密码问题。
#include <iostream>
#include <string>
using namespace std;
void encode(string & str)
{
int i,j,len = str.length();
for(i=0;i<len;i++)
{
str[i] += 3;
j = str[i] - 'Z';
if(j>=1 && j<=3)
{
str[i] = 'A'-1 + j;
continue;
}
j = str[i] - 'z';
if(j>=1 && j<=3)
{
str[i] = 'a'-1 + j;
continue;
}
}
}
void decode(string & str)
{
int i,j,len = str.length();
for(i=0;i<len;i++)
{
str[i] -= 3;
j = 'A'-str[i];
if(j>=1 && j<=3)
{
str[i] = 'Z' + 1 - j;
continue;
}
j = 'a'-str[i];
if(j>=1 && j<=3)
{
str[i] = 'z' + 1- j;
continue;
}
}
}
int main()
{
string A,B;
while(cin>>A)
{
if(A == "END")
break;
cin>>B;
if(A == "ENCODE")
encode(B);
else
decode(B);
cout<<B<<endl;
}
system("PAUSE");
}