题目来源:百练2913 加密技术
描述
编制程序,将输入的一行字符加密解密。加密时,每个字符依次反复加上“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
输入
输入一行字符串,其中包含了若干空格。
输出
对输入字符串进行加密,并输出加密结果。
再对输入字符串进行解密,并在换行后输出解密结果。
样例输入
aghi lrtq haha
样例输出
epnk(suxz&"phke
aghi lrtq haha
#include <iostream>
#include <cstring>
using namespace std;
int a[7] = {4, 9, 6, 2, 8, 7, 3};
int main()
{
char str[100];
gets(str);
int len = strlen(str);
for (int i = 0; i < len; i++)
{
str[i] = str[i] + a[i % 7];
if (str[i] > 132)
{
str[i] = str[i] % 90 + 32;
}
}
cout << str << endl;
for (int i = 0; i < len; i++)
{
str[i] = str[i] - a[i % 7];
}
cout << str << endl;
return 0;
}