You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

50 lines
1.7 KiB

2 years ago
#include <bits/stdc++.h>
using namespace std;
//https://zhuanlan.zhihu.com/p/265677795
char encoder[26] = {'C', 'S', 'P'};//声明26个长度的字符数组只初始化了前三个后面23个默认是0即 \000
char decoder[26]; //声明一个字符数组没有进行初始化默认值是0,即 \000
string st;
int main() {
int k = 0;
for (int i = 0; i < 26; i++)
if (encoder[i] != 0) ++k; //因为有三个进行了初始化所以k=3
for (char x = 'A'; x <= 'Z'; ++x) { //x的意思是遍历每一个大写字母
bool flag = true;
for (int i = 0; i < 26; ++i)
if (encoder[i] == x) {
flag = false; //如果在 encoder数组中找到则 flag=false,并且停止循环
break;
}
if (flag) { //如果没有找到,那么依次填充到encoder数组中
encoder[k] = x;
++k;
}
}
//根据encoder换算出decoder
for (int i = 0; i < 26; ++i) {
decoder[encoder[i] - 'A'] = i + 'A'; //这句加密算法是核心
}
//这里应该输出一下decoder,就清楚了,如果是笔试那么需要用笔把26个都描述出来
for (int i = 0; i < 26; ++i) {
cout << "encoder[" << i << "]=" << encoder[i] << " " << endl;
}
for (int i = 0; i < 26; ++i) {
cout << "decoder[" << i << "]=" << decoder[i] << " " << endl;
}
for (int i = 0; i < 26; i++) {
cout << char('A' + i) << ":" << i << " ";
}
cout << endl;
//输入一个字符串,遍历每一个字符,进行解码
cin >> st;
for (int i = 0; i < (int)st.length(); ++i)
st[i] = decoder[st[i] - 'A'];
cout << st;
return 0;
}