Playground
//koristi C++11 - http://mendo.mk/Lecture.do?id=26 #include <iostream> #include <vector> #include <set> using namespace std; void print(vector<int> &permutation) { for (auto value : permutation) { cout << value << " "; } cout << endl; } void create(vector<int> &permutation, set<int> &used, int nextIndex) { int N = permutation.size(); //dali sme gi postavile site elementi? if(nextIndex >= permutation.size()) { print(permutation); return; } else { //isprobaj ja sekoja mozhna vrednost //za postavuvanje vo permutation[nextIndex] for(int i=1; i <= N; i++) { if(used.count(i) == 0) { //ne sme ja iskoristile taa vrednost, zemi ja sega used.insert(i); permutation[nextIndex] = i; create(permutation, used, nextIndex+1); //od koga ja isprobavme vrednosta, oslobodi ja istata, //bidejki sega, so slednoto izvrshuvanje na for-ciklusot, //na ovaa pozicija kje stavime neshto drugo used.erase(i); permutation[nextIndex] = -1; } } } } int main() { set<int> used; vector<int> permutation; //sozdavame niza od 5 elementi, //pa kje napravime permutacii od 5 elementi permutation.resize(5); create(permutation, used, 0); return 0; }
Input data
Program output
Execute
Language: ????????? | Memory: ???? KB | Time: ??? ms
Nothing has been executed, yet!