c++ - Check for every rugby score the recursive way without repetitions -
For fun I have created an algorithm that calculates every possible combination from a given rugby score (3, 5 or 7 digits). I found two methods: First there is a brute force, 3 for imbricated
to loop second is a recursive. Problems Some combinations appear many times how can I avoid it?
My code:
#include & lt; Iostream & gt; using namespace std; Zero computeScore (int score, int nbTryC, int nbTryNC, int nbPenalties); Int main () {int score = 0; While (true) {cout & lt; & Lt; "Enter score:"; Cin & gt; & Gt; Score; Cout & lt; & Lt; "---------------" & lt; & Lt; Andal & lt; & Lt; "Score =" & lt; & Lt; Score & lt; & Lt; Andal & lt; & Lt; "---------------" & lt; & Lt; Endl; // Recurring call calculation score (Score, 0, 0, 0); } Return 0; } Zero computeScore (int score, int nbTryC, int nbTryNC, int nbPenalties) {int tryC = 7; Const int tryNC = 5; Const int penalty = 3; If (score == 0) {cout & lt; & Lt; "* Attempts:" & lt; & Lt; NbTryC & lt; & Lt; "| TRIPS NT:" & lt; & Lt; NbTryNC & lt; & Lt; "Penalties / Drops:" & lt; & Lt; NbPenalties & lt; & Lt; Endl; Cout & lt; & Lt; "---------------" & lt; & Lt; Endl; } And if (score & lt; penalty) {// invalid combination} and {computeScore (score - tryC, nbTryC + 1, nbTryNC, nbPenalties); ComputeScore (Score - tryNC, nbTryC, nbTryNC + 1, nbPenalties); ComputeScore (Score - Penalties, NBTIC, NBTRENC, NB Penneline + 1); }}
One way to think about this is by any time you have Yoga, you can sort it all the values into some "canonical" form. For example,
20 = 5 + 7 + 3 + 5 you also get it as
Can write. 20 = 7 + 5 + 5 + 3 This gives some different options to solve your problem. First of all, you can always sort and record the amount that you do, can never output the same amount twice. There is a problem in it that you are repeatedly creating the same amount of many different times, which is very inefficient.
The second (more preferable) way to do this is to update the recursion in a slightly different way. Now, your rating works by adding 3, 5 and 7 in each step, this is the same Who got out of order at first place. An alternative approach will be to think about adding all those 7s, which you want to add, then all 5, then all 3. In other words, your recursion will do something like this:
kValues = {7, 5, 3} function RecursivelyMakeTarget (target, value, index) {// here, target target To create, the number of values is 7, 5, 5 and 3, and the index is the index of the number that you add. // Base case: If we overhaul the goal, then we have done (target & lt; 0) return; // Base case: If we use each number but it is not made, then we have done (index == length (kValues)) return; // Base case: If we create goals, then we have done (target == 0) print value; Return; // Otherwise, we have two options: // 1. Add the current number to the target. // 2. Say that we are using the current number. // case one value [index] ++; RecursivelyMakeTarget (target - kValues [index], value, index); Values [index] -; // Episode Two Recurring Measure Target (Target, Cost, Index + 1); } Function makeget (target) {recurring mac target (target, [0, 0, 0], 0); } The idea here is that you want to add all 5, before you add it to any 5, and add it to 5 before adding it to any 5. If you look at the recurring tree size made in this way, you will know that no two paths can be attempted to use the same amount, because when the path is added to a different number in the branches Was used to start or use recycling was the next number in the series, as a result, each amount is generated at once, and no duplicate usage is done. .
In addition to this, it is the scales to work with any possible values to add the above approach, so if rugby introduces a new Super Goyal 15 points, you can simply kValues update the array and everything will work properly. Hope it helps!
Comments
Post a Comment