Last active
December 10, 2019 19:11
-
-
Save mkondratek/2e074baaf2959b416ac1d8fd73e7202f to your computer and use it in GitHub Desktop.
Losowanie prezentów
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <vector> | |
#include <algorithm> | |
#include <stdlib.h> | |
int getRandom(int a, int b) { | |
return a + (rand() % (b - a)); | |
} | |
bool isOk(std::vector<int> const& vec) { | |
for (int i = 0; i < vec.size(); ++i) { | |
if (i == vec[vec[i]]) { | |
return false; | |
} | |
} | |
return true; | |
} | |
std::vector<int> permutation(int n) { | |
std::vector<int> vec(n); | |
while (n--) { | |
vec[n] = n; | |
} | |
int forceTimes = n * n - 1; | |
while (forceTimes > 0 || !isOk(vec)) { | |
int x = getRandom(0, vec.size()); | |
int y = getRandom(0, vec.size()); | |
if (x != y) { | |
std::swap(vec[x], vec[y]); | |
forceTimes--; | |
} | |
} | |
return vec; | |
} | |
int main(int argc, char** argv) { | |
std::vector<std::string> data; | |
std::string line; | |
srand(time(NULL)); | |
while (std::getline(std::cin, line)) { | |
data.push_back(line); | |
} | |
std::sort(data.begin(), data.end()); | |
std::vector<int> perm = permutation(data.size()); | |
for (int i = 0; i < data.size(); ++i) { | |
std::string cmd = "printf \"" + data[i] + " \" &&" + | |
"echo \"openssl enc -base64 <<< '" + data[perm[i]] + "'\" | bash"; | |
std::system(cmd.c_str()); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment