JOJ1064: Caeser Comes Back

来源:互联网 发布:mysql入门教程视频 编辑:程序博客网 时间:2024/06/05 08:53

原题地址: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");
   
}

原创粉丝点击