Algorithm


ALGORITHM

Consensus algorithm

Consensus algorithm

Question

A group of ten people need to decide which one flavor of ice cream they will all order, out of three options. The algorithm can question and re-question the participants, and present the answers to the participants, until a consensus is reached. This exercise is somewhat more open-ended. Add your assumptions if necessary. Obviously, this algorithm might never result in an answer. Deal with that too.

Answer

Combinations with repetition

void Enumerate5CardCombinationsWithRepetition(){    int totalOutcomesEnumerated = 0;    for (int card1 = 0; card1 < 52; card1++)    {    for (int card2 = card1; card2 < 52; card2++)    {    for (int card3 = card2; card3 < 52; card3++)    {    for (int card4 = card3; card4 < 52; card4++)    {    for (int card5 = card4; card5 < 52; card5++)    {  totalOutcomesEnumerated++;    }    }    }    }    }}

Total five-card combinations enumerated with this loop: 3,819,816.

Combinations without repetition

void Enumerate5CardCombinations(){    int totalOutcomesEnumerated = 0;    for (int card1 = 0; card1 < 48; card1++)    {  ...
Related Ads