I hope this code helps someone with iterative permutation (it is not simple),but with basic source-code and use only one basic library, this permutation only works for 4 elements (Absolutely for beginners who wants a challenge)It took me 2 days for doing this code, because I didn't want help of how to create my own permutation code, even though it is not perfect because of the length of the array, however I understood the point. I will leave you the code if you catch the idea you will be able to write an iterative permutation code for more elements, and also inputting the elements :D.
#include <iostream>
int main() {
for (char v = 'A'; v <= 'D'; v ++)
for (char x = 'A'; x <= 'D'; x ++)
for (char y = 'A'; y <= 'D'; y ++)
for (char z = 'A'; z <= 'D'; z ++)
if (v != x && v != y && v != z && x != y && x != z && y != z)
std::cout << v << " " << x << " " << y << " " << z << "\n";
return 0;
}
Could be modified for the permutations of the array easy enough, but there is a simpler way using for loops and a single if statement. Just trying to help you broaden your horizons.
Volatile_Pusle Thanks :D that was new for me, and giblit I didn't want to use the algorithm library or recursively function, coz I think it is much more easy to do it at that way, don't you think? I was trying to do it iterively.
And once again, thanks Volatile_Pulse for your help, but when you are going to use array you could not use it with your code. :p.
but when you are going to use array you could not use it with your code. :p.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#include <iostream>
int main() {
char letters[] = {'A', 'B', 'C', 'D'};
for (int v = 0; v < 4; v ++)
for (int x = 0; x < 4; x ++)
for (int y = 0; y < 4; y ++)
for (int z = 0; z < 4; z ++)
if (v != x && v != y && v != z && x != y && x != z && y != z)
std::cout << letters[v] << " " << letters[x] << " " << letters[y] << " " << letters[z] << "\n";
return 0;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#include <iostream>
int main() {
std::string letters = "ABCD";
for (int v = 0; v < 4; v ++)
for (int x = 0; x < 4; x ++)
for (int y = 0; y < 4; y ++)
for (int z = 0; z < 4; z ++)
if (v != x && v != y && v != z && x != y && x != z && y != z)
std::cout << letters[v] << " " << letters[x] << " " << letters[y] << " " << letters[z] << "\n";
return 0;
}
;D kool, now i need to learn everything about recursive, do you know some good books that only talk about recursive funcion ? i mean only recursive to go deeply or maybe a forum with many examples explaining them.
Long time ago, I was bored. So I decided to do what you did, except I wanted to copy what std::next_permutation does. So I found some tutorials and I made this:
It even comes with it's own cute template sorting function (bubble sort) and it's own swap function. And for some reason, I used a lot of underscores back then to.
Not to long ago, I found a permutation algorithm that is able to find the nth permutation of a set of values; but this one only works for strings of length no more than 20 because 20! is larger than longlong. But if done in java, you can use BigInteger library and it should be good to go