Combinations on C++ | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Combinations on C++

So, i did a combination program on python (using itertools library) for an automation program, but now (since i'm learning c++) i want do to it on c++, but idk the logic behind the itertools part of the code and that's what i'm trying to find so i can "convert" it to c++ code. To better explain what i mean by dont understanding the logic, My python program does this steps: -Gets Input A and B (integer input) -Defines an array with the same size as Input B (so if input is like 5, the array has size 5) -Goes through the array using a loop and attributes to each "slot" of the array it's number, so array[i] = i -Then it uses itertools product and chain methods like this: combination = list(itertools.product(arrayName, repeat=InputA)) result = itertools.chain(combination) And idk what it does (and is in this part that i am confused) but going through "result" will return all the possible combinations. One thing to note: 0/0/1/0 and 0/0/0/1 are different combinations, and if input B is a higher number like 3 or 4 it has to go through all the numbers like 0/4/1/2 Why i'm point this out? I've trying to search this on the internet and on most "combinations solving" posts for them this doesn't matter, 0/0/1/0 is the same as 0/0/0/1 and their combinations don't go through all of them, like instead of return 0/0/3/2 it will instead return 0/0/3/4 This is my python code if it helps to understand what i'm trying to achieve (i'm on pc, not sure if this link works on mobile) https://www.sololearn.com/compiler-playground/cHv64BoCgpQt/#py

18th Nov 2022, 8:18 PM
LethalArms
LethalArms - avatar
1 Answer
- 1
LethalArms I think the mathematical model is to count in a given base. If I am correct, A+1 is the base and B is the number of columns or placeholders. A=2 => 2+1 = base 3 counting (radix 3) B=3 => 3 placeholders 0 0 0 0 0 1 0 0 2 0 1 0 0 1 1 0 1 2 0 2 0 0 2 1 0 2 2 1 0 0 1 0 1 1 0 2 1 1 0 1 1 1 1 1 2 1 2 0 1 2 1 1 2 2 2 0 0 2 0 1 2 0 2 2 1 0 2 1 1 2 1 2 2 2 0 2 2 1 2 2 2 Think how you might model counting up and handling carryovers from column to column in any base (A+1) for an arbitrary number of columns (B) until the highest column overflows. There are (A+1)^B combinations.
19th Nov 2022, 1:17 AM
Brian
Brian - avatar