I need to get all the possible combinations of a given set. For example, given: [1, 4, 7], the resulting combinations should be:
I tried using next_permutation
method, but it isn't what I want (this does not return values like 111, 144, 717, etc).
So is there any way I can do this in C++? Please note that I'm a complete beginner.
Thanks in advance.
Have a hard look at the numbers: All numbers you listed can also be expressed as the list {11,14,17,41,44,47,71,74,77} prefixed once with 1, once with 4 and once with 7. This points to a generic rule:
The strings with 3 numbers of the set {1,4,7} are built by taking the strings with 2 numbers of the same set and prepending each element of the set.
Generalize the 3 and 2 in this rule, implement the resulting idea with recursion, and you have an algorithm for your problem.
As a C++ implementation note, make sure that you use strings instead of integers to represent your numbers. This problem is not arithmetic, and tightly coupled to the base-10 representation. Strings will make your life much easier.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With