『宏的使用』题解 | #字符串加解密#

字符串加解密

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

#include<bits/stdc++.h>
using namespace std;

#define smallchar 'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'
#define bigchar 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'
#define number '0','1','2','3','4','5','6','7','8','9'
//'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'
//'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'
//'0','1','2','3','4','5','6','7','8','9'
map<char,char> first;
map<char,char> second;

void init()
{
    char small[26]={smallchar};
    char big[26]={bigchar};
    char num[10]={number};

    //编码
    for(char c='a'; c<='z'; ++c)
    {
        first[ c ]=big[ (c-'a'+1)%26 ];
    }
    for(char c='A'; c<='Z'; ++c)
    {
        first[ c ]=small[ (c-'A'+1)%26 ];
    }
    for(char c='0'; c<='9'; ++c)
    {
        first[c]=num[ (c-'0'+1)%10 ];
    }

    //解码
    for(char c='a'; c<='z'; ++c)
    {
        second[ c ]=big[ (c-'a'-1+26)%26 ];
    }
    for(char c='A'; c<='Z'; ++c)
    {
        second[ c ]=small[ (c-'A'-1+26)%26 ];
    }
    for(char c='0'; c<='9'; ++c)
    {
        second[c]=num[ (c-'0'-1+10)%10 ];
    }    
}

int main()
{
    init();
    string str;
    while( getline(cin,str) )
    {
        for(int i=0; i<str.size(); ++i)
        {
            str[i]=first[ str[i] ];
        }
        cout<<str<<endl;
        str.clear();
        //cin>>str;
        getline(cin,str);
        for(int i=0; i<str.size(); ++i)
        {
            str[i]=second[ str[i] ];
        }
        cout<<str<<endl;
        str.clear();
    }


    return 0;
}
全部评论

相关推荐

1 1 评论
分享
牛客网
牛客企业服务