cpp代码如下
#include <iostream>using namespace std
string encode(const string &input, size_t key) {
string result
size_t size = input.size()
for (size_t col = 0 col < key ++col)
for (size_t cur = col cur < size cur += key)
result += input[cur]
return result
}
string decode(const string &input, size_t key) {
size_t size = input.size()
string result('?', size)
for (size_t off = 0, pos = 0, col = 0 off < size ++off, pos += key) {
if (pos >= size)
pos = ++col
result[pos] = input[off]
}
return result
}
int main() {
string orig = "abcdefghijklmnopqrstuvwxyz"
size_t key = 7
string encoded = encode(orig, key)
string decoded = decode(encoded, key)
cout << "orig: " << orig << endl
cout << "enc: " << encoded << endl
cout << "dec: " << decoded << endl
return 0
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)