Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6471075
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T06:10:59+00:00 2026-05-25T06:10:59+00:00

Just for fun I created an algorithm that computes every possible combination from a

  • 0

Just for fun I created an algorithm that computes every possible combination from a given rugby score (3, 5 or 7 points). I found two methods : The first one is brute force, 3 imbricated for loops. The other one is recursion.

Problem is some combinations appear multiple times. How can I avoid that ?

My code :

#include <iostream>
using namespace std;
void computeScore( int score, int nbTryC, int nbTryNC, int nbPenalties );

int main()
{
    int score = 0;
    while (true)
    {
        cout << "Enter score : ";
        cin >> score;
        cout << "---------------" << endl << "SCORE = " << score << endl
                << "---------------" << endl;

        // Recursive call
        computeScore(score, 0, 0, 0);
    }
    return 0;
}

void computeScore( int score, int nbTryC, int nbTryNC, int nbPenalties )
{
    const int tryC = 7;
    const int tryNC = 5;
    const int penalty = 3;

    if (score == 0)
    {
        cout << "* Tries: " << nbTryC << " | Tries NT: " << nbTryNC
                << " | Penal/Drops: " << nbPenalties << endl;
        cout << "---------------" << endl;
    }
    else if (score < penalty)
    {
        // Invalid combination
    }
    else
    {
        computeScore(score - tryC, nbTryC+1, nbTryNC, nbPenalties);
        computeScore(score - tryNC, nbTryC, nbTryNC+1, nbPenalties);
        computeScore(score - penalty, nbTryC, nbTryNC, nbPenalties+1);
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T06:11:00+00:00Added an answer on May 25, 2026 at 6:11 am

    One way to think about this is to realize that any time you have a sum, you can put it into some “canonical” form by sorting all the values. For example, given

    20 = 5 + 7 + 3 + 5
    

    You could also write this as

    20 = 7 + 5 + 5 + 3
    

    This gives a few different options for how to solve your problem. First, you could always sort and record all of the sums that you make, never outputting the same sum twice. This has the problem that you’re going to end up repeatedly generating the same sums multiple different times, which is extremely inefficient.

    The other (and much better) way to do this is to update the recursion to work in a slightly different way. Right now, your recursion works by always adding 3, 5, and 7 at each step. This is what gets everything out of order in the first place. An alternative approach would be to think about adding in all the 7s you’re going to add, then all the 5’s, then all the 3’s. In other words, your recursion would work something like this:

     Let kValues = {7, 5, 3}
    
     function RecursivelyMakeTarget(target, values, index) {
          // Here, target is the target to make, values are the number of 7's,
          // 5's, and 3's you've used, and index is the index of the number you're
          // allowed to add.
    
          // Base case: If we overshot the target, we're done.
          if (target < 0) return;
    
          // Base case: If we've used each number but didn't make it, we're done.
          if (index == length(kValues)) return;
    
          // Base case: If we made the target, we're done.
          if (target == 0) print values; return;
    
          // Otherwise, we have two options:
          // 1. Add the current number into the target.
          // 2. Say that we're done using the current number.
    
          // Case one
          values[index]++;
          RecursivelyMakeTarget(target - kValues[index], values, index);
          values[index]--;
    
          // Case two
          RecursivelyMakeTarget(target, values, index + 1);
     }
    
     function MakeTarget(target) {
          RecursivelyMakeTarget(target, [0, 0, 0], 0);
     }
    

    The idea here is to add in all of the 7’s you’re going to use before you add in any 5’s, and to add in any 5’s before you add in any 3’s. If you look at the shape of the recursion tree that’s made this way, you will find that no two paths end up trying out the same sum, because when the path branches either a different number was added in or the recursion chose to start using the next number in the series. Consequently, each sum is generated exactly once, and no duplicates will be used.

    Moreover, this above approach scales to work with any number of possible values to add, so if rugby introduces a new SUPER GOAL that’s worth 15 points, you could just update the kValues array and everything would work out just fine.

    Hope this helps!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing some code (just for fun so far) in Python that will store
Fun with enums in C#. Take one generic list that is created to store
I recently created an auto browser windows form app that browsers various places just
Java Day 1 from c#. Just playing around with java (just for fun) and
Just for fun, how close can we get to debug an application in C#
I built (just for fun) 3 classes to help me log some events in
I'm someone who writes code just for fun and haven't really delved into it
I'm learning about AI and (just for fun and practice, not profit or anything
I've been developing a Smalltalk variant for just the fun of it and I
I want to develop simple Serverless LAN Chat program just for fun. How can

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.