'commit'
This commit is contained in:
49
TangDou/CSP-J/J1_2020/2.cpp
Normal file
49
TangDou/CSP-J/J1_2020/2.cpp
Normal file
@@ -0,0 +1,49 @@
|
||||
#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;
|
||||
}
|
||||
Reference in New Issue
Block a user