简体   繁体   中英

return number of solutions to the equation x1 + x2 + x3 = num

I am trying to write a recursive method that accepts as a positive integer num parameter and returns the number of solutions to the equation

x1 + x2 + x3 = num

When the three xs are positive integers between 1 and 10. The method should also print out these solutions, each solution in a separate line. The print order does not matter.

I tried:

public static int solutions (int x1, int x2, int x3, int counter, int num)
{
    if(x1 > 10 || x2 > 10 || x3 > 10) {
        return 0;
    }
    if (x1 + x2 + x3 == num) {
        System.out.println(x1 + "+" + x2 + "+" + x3);
        counter = 1;
    } else {
        return solutions (x1 + 1, x2, x3, counter, num) +
               solutions (x1, x2 + 1, x3, counter, num) +
               solutions (x1, x2, x3 + 1, counter, num);
    }
    return counter;
}

public static int solutions (int num)
{
    if (num < 3 || num > 30) {
        return 0;
    }
    return solutions (1, 1, 1, 0, num);

}

The problem is that I repeat the results, The problem is that I get repeated results, for example num = 5, I get:

3 + 1 + 1
2 + 2 + 1
2 + 1 + 2
2 + 2 + 1
1 + 3 + 1
1 + 2 + 2
2 + 1 + 2
1 + 2 + 2
1 + 1 + 3

Instead of

1 + 1 + 3
1 + 2 + 2
1 + 3 + 1
2 + 1 + 2
2 + 2 + 1
3 + 1 + 1

How can I avoid repeating it twice?

Here is how I'd approach it:

public static void main(String[] args) {
    System.out.println("Number of solutions: "+ solutions(5));
}

public static int solutions(int num) 
{
    if (num < 3 || num > 30)
        return 0;
    else
        return solutions(num, 1, 1, 1);
}

private static int solutions(int num, int x1, int x2, int x3)
{   
    int valid = 0;          
    if (x1 + x2 + x3 == num)
    {
        valid = 1;
        System.out.println(x1 + " + " + x2 + " + " + x3 + " = " + num);            
    }                
    if ((x3 < 10) && (x1 + x2 + x3 < num))
    {
        return valid + solutions(num, x1, x2, ++x3);
    }
    else if ((x2 < 10) && (x1 + x2 < num)) 
    {
        return valid + solutions(num, x1, ++x2, 1);    
    }
    else if ((x1 < 10) && (x1 < num))
    {
        return valid + solutions(num, ++x1, 1, 1);
    }  
    else
    {
        return valid;
    }
}

This is definitely expected behaviour for the code you've provided; the first time your function is called it is going to do this (I've removed the last two arguments for the sake of brevity):

return solutions(2, 1, 1) + solutions(1, 2, 1) + solutions(1, 1, 2)

Given that for all three arguments you are adding 1 until you get to 10, you would expect (for example) 2 + 2 + 1 to be produced by both the first and the second call at some point.

One solution to your problem would be to pass a collection object of some kind through the recursion which you add solutions to each time you find one. Then when you find a new one you check if is contained in your collection as well as checking if it adds up to the correct number.

This is what I came up with... (it works with any amount of variables in the equation)

public static int solutions(int num)
{
    return solutions(num, 3, 1, ""); // 'varsInTheEq' represents the numbers of varibelas in the equation (in this case: 'x1 + x2 + x3 = num' which is 3)
}

private static int solutions(int num, int varsInTheEq, int value, String builder)
{
    if (num == 0 && varsInTheEq == 0) // if we solved the equation
    {
        System.out.println(builder);
        return 1;
    }

    if (varsInTheEq == 0 || value > num)
        return 0;

    int r1 = solutions(num - value, varsInTheEq - 1, 1, builder + value + " ");
    int r2 = solutions(num, varsInTheEq, value + 1, builder);

    return r1 + r2;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM