注解
1、模拟题。建立四个数组,分别是cipherText,cipherCode,plainText以及plainCode,按照题目叙述对cipherText首先编码成cipherCode,然后根据k值和i值(位置)进行映射和变换,变为plainCode,最后再变换成plainText。
代码
#include <iostream>
using namespace std;
int main() {
string map = "_abcdefghijklmnopqrstuvwxyz.";
int k;
cin>>k;
while(k) {
string cipherText;
cin>>cipherText;
int cipherCode[cipherText.length()];
int plainCode[cipherText.length()];
for(int i=0; i<cipherText.length(); i++) {
for(int j=0; j<map.length(); j++) {
if(map.at(j)==cipherText.at(i)) {
cipherCode[i] = j;
}
}
}
for(int i=0; i<cipherText.length(); i++) {
int pos = (k*i) % cipherText.length();
plainCode[pos] = ((cipherCode[i] + 28) % 28 + i) % 28;
}
string ans = "";
for(int i=0; i<cipherText.length(); i++) {
ans += map.at(plainCode[i]);
}
cout<<ans<<endl;
cin>>k;
}
return 0;
}