There is an array of letters ABCGD . It is necessary to find all variants of permutations, i.e. AB , AB , ABC , AVDG , ABCD , from two to five (in this case) characters.
I tried to use std::next_permutation , but this is not exactly what is required, since, for example, with three characters, it displays only three-character combinations.
I decided this when the output, but there was a new problem. As soon as two asterisks appear in the array (which is also necessary under the conditions of the problem), std::next_permutation stops working. What could be the problem and how to solve it?
int Fact(int n) { if (n == 2 || n == 1) { return n; } else { return n * Fact(n - 1); } } void PrintC(char*arr, int len, int ind) { for (int i = 0; i < len; ++i) { cout << arr[i]; } cout << '\t'; for (int i = 2; i < len; i++) { if (len > 1 && ind % Fact(len - i) == 0) { PrintC(arr, len - 1, ind); } } } int len = 3; char* arr = new char[len + 1]; cin.getline(arr, len + 1); sort(&arr[0], &arr[len]); int ind = 1; do { cout << ind << endl << endl; PrintC(arr, len, ind); cout << endl; ind++; } while (next_permutation(&arr[0], &arr[len])); _getch(); return;
next_permutationdeals exactly with that permutations. - ixScinext_permutationno problems withnext_permutation, just this function is intended for permutations, not for placements. - ixSci