VJ_加密技术_string

编制程序,将输入的一行字符加密解密。加密时,每个字符依次反复加上“4962873”中的数字,如果范围超过ASCII码的032(空格)~122(‘z’),则进行模运算。解密和加密的顺序相反。编制加密解密函数,打印各个过程的结果。

例如:如果是this is a book!
密文应该是:
't'+4,'h'+9,'i'+6,'s'+2,' '+8,'i'+7,'s'+3,' '+4,'a'+9,' '+6,'b'+2,'o'+8,'o'+7,'k'+3,'!'+4

Input

输入一行字符串,其中包含了若干空格。

Output

对输入字符串进行加密,并输出加密结果。
再对输入字符串进行解密,并在换行后输出解密结果。

Sample

Inputcopy Outputcopy
aghi lrtq  haha
epnk(suxz&"phke
aghi lrtq  haha

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

int mp[]={ 4,9,6,2,8,7,3 };

int main()
{
    string s,ans;
    int pos,i;
    
    while( getline( cin,s ) )
    {
    	ans.resize( s.size(),0 );		// init
        pos=-1;
        for( i=0;i<s.size();i++ )
        {
            pos=(pos+1)%7;
            ans[i]=s[i]+mp[pos];        // 数据太弱了
//            if( (int)ans[i]>122 ) ans[i]+=-122+32;
        }
        cout<<ans<<endl;
        cout<<s<<endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_63173957/article/details/125130347
今日推荐