#include <algorithm>
#include <array>
#include <chrono>
#include <iostream>
#include <iterator>
#include <numeric>
#include <random>
constexpr size_t n = 20;
using namespace std;
using namespace std::chrono;
using value_t = unsigned;
using array_t = array<value_t, n>;
void show(const char* prompt, const array_t& s, const size_t k) {
cout << prompt;
auto osi = ostream_iterator<value_t>(cout, " ");
const auto s_end = begin(s) + k;
copy(begin(s), s_end, osi);
cout.put('\n');
}
void show(const char* prompt, const value_t value) {
cout << prompt << value << '\n';
}
void encrypt(value_t& value, const array_t& s, const size_t k) {
const auto s_end = begin(s) + k;
const auto pos = find(begin(s), s_end, value);
value = static_cast<value_t>(distance(begin(s), pos));
}
void encrypt(array_t& m, const array_t& s, const size_t k) {
const auto m_end = begin(m) + k;
for (auto& v : m) encrypt(v, s, k);
}
value_t decrypt(const array_t& m, const array_t& s, const size_t i) {
return s[m[i]];
}
value_t parse(const char* prompt, const value_t lim) {
value_t value{};
while (!value || lim < value) {
cout << prompt;
cin >> value;
cin.ignore(0x1000, '\n');
}
return value;
}
int main() {
constexpr auto lim = static_cast<value_t>(n);
const auto k = parse("k: ", lim);
uniform_int_distribution<> uid(1, k);
mt19937 gen{ random_device()() };
array_t m{};
auto rand = [&uid, &gen](value_t& x) { x = uid(gen); };
const auto m_end = begin(m) + k;
for_each(begin(m), m_end, rand);
show("M: ", m, k);
array_t s{};
const auto s_end = begin(s) + k;
iota(begin(s), s_end, 1);
const auto seed = static_cast<unsigned>(system_clock::now().time_since_epoch().count());
shuffle(s.begin(), s_end, default_random_engine(seed));
show("S: ", s, k);
encrypt(m, s, k);
show("M: ", m, k);
const auto i = size_t(parse("i: ", lim - 1));
const auto p = decrypt(m, s, i);
show("P: ", p);
}
int N = 20; int k = 11;
int M[N - 1] = { 1,2,5,8,2,5,8,10,11,1,3,1,11,10,1,11,7,7,3,1 }; тут возникает ошибка
int MM[N - 1] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 };
int S[k - 1] = { 5,4,3,2,1,7,8,9,10,11,6 };
cout << "Массив M " << N << " элементов" << endl;
for (int i = 0; i < N; i++) cout << M[i] << " ";
cout << endl;
cout << "Массив S " << k << " элементов" << endl;
for (int i = 0; i < k; i++) cout << S[i] << " ";
cout << endl;
cout << "Массив до шифрования " << N << " элементов" << endl;
for (int i = 0; i < N; i++) cout << MM[i] << " ";
cout << endl;
int q;
for (int i = 0; i < N; i++)
{
q = M[i];
MM[i] = S[q - 1];
}
cout << "Массив после шифрования " << N << " элементов" << endl;
for (int i = 0; i < N; i++) cout << MM[i] << " ";
}