📜  和方程的非负积分解的数量

📅  最后修改于: 2021-04-24 15:17:41             🧑  作者: Mango

给定一个数n(变量数)和val(变量之和),找出有多少个这样的非负积分解是可能的。
例子 :

Input : n = 5, val = 1
Output : 5
Explanation:
x1 + x2 + x3 + x4 + x5 = 1
Number of possible solution are : 
(0 0 0 0 1), (0 0 0 1 0), (0 0 1 0 0),
(0 1 0 0 0), (1 0 0 0 0)
Total number of possible solutions are 5

Input : n = 5, val = 4
Output : 70
Explanation:
x1 + x2 + x3 + x4 + x5 = 4
Number of possible solution are: 
(1 1 1 1 0), (1 0 1 1 1), (0 1 1 1 1), 
(2 1 0 0 1), (2 2 0 0 0)........ so on......
Total numbers of possible solutions are 70

提问人:微软访谈

1.进行递归函数调用countSolutions(int n,int val)
2.调用此解决方案函数countSolutions(n-1,val-i)直到n = 1且val> = 0,然后返回1。
下面是上述方法的实现:

C++
// CPP program to find the numbers
// of non negative integral solutions
#include
using namespace std;
 
// return number of non negative
// integral solutions
int countSolutions(int n, int val)
{
    // initialize total = 0
    int total = 0;
 
    // Base Case if n = 1 and val >= 0
    // then it should return 1
    if (n == 1 && val >=0)
        return 1;
 
    // iterate the loop till equal the val
    for (int i = 0; i <= val; i++){
         
        // total solution of equations
        // and again call the recursive
        // function Solutions(variable,value)
        total += countSolutions(n-1, val-i);
         
    }
     
    // return the total no possible solution
    return total;
}
 
// driver code
int main(){
     
    int n = 5;
    int val = 20;
     
    cout<


Java
// Java program to find the numbers
// of non negative integral solutions
class GFG {
 
  // return number of non negative
  // integral solutions
  static int countSolutions(int n, int val) {
 
    // initialize total = 0
    int total = 0;
 
    // Base Case if n = 1 and val >= 0
    // then it should return 1
    if (n == 1 && val >= 0)
      return 1;
 
    // iterate the loop till equal the val
    for (int i = 0; i <= val; i++) {
 
      // total solution of equations
      // and again call the recursive
      // function Solutions(variable, value)
      total += countSolutions(n - 1, val - i);
    }
 
    // return the total no possible solution
    return total;
  }
 
  // Driver code
  public static void main(String[] args) {
    int n = 5;
    int val = 20;
 
    System.out.print(countSolutions(n, val));
  }
}
 
// This code is contributed by Anant Agarwal.


Python3
# Python3 program to find the numbers
# of non negative integral solutions
 
# return number of non negative
# integral solutions
def countSolutions(n, val):
 
    # initialize total = 0
    total = 0
 
    # Base Case if n = 1 and val >= 0
    # then it should return 1
    if n == 1 and val >=0:
        return 1
 
    # iterate the loop till equal the val
    for i in range(val+1):
     
        # total solution of equations
        # and again call the recursive
        # function Solutions(variable,value)
        total += countSolutions(n-1, val-i)
 
    # return the total no possible solution
    return total
 
# driver code
n = 5
val = 20
print(countSolutions(n, val))


C#
// C# program to find the numbers
// of non negative integral solutions
using System;
 
class GFG {
 
    // return number of non negative
    // integral solutions
    static int countSolutions(int n, int val) {
     
        // initialize total = 0
        int total = 0;
     
        // Base Case if n = 1 and val >= 0
        // then it should return 1
        if (n == 1 && val >= 0)
        return 1;
     
        // iterate the loop till equal the val
        for (int i = 0; i <= val; i++) {
         
            // total solution of equations
            // and again call the recursive
            // function Solutions(variable, value)
            total += countSolutions(n - 1, val - i);
        }
     
        // return the total no possible solution
        return total;
    }
     
    // Driver code
    public static void Main()
    {
        int n = 5;
        int val = 20;
     
        Console.WriteLine(countSolutions(n, val));
    }
}
 
// This code is contributed by Anant vt_m.


PHP
= 0 then it should
    // return 1
    if ($n == 1 && $val >=0)
        return 1;
 
    // iterate the loop
    // till equal the val
    for ($i = 0; $i <= $val; $i++)
    {
         
        // total solution of equations
        // and again call the recursive
        // function Solutions(variable,value)
        $total += countSolutions($n - 1,
                                 $val - $i);
         
    }
     
    // return the total
    // no possible solution
    return $total;
}
 
// Driver Code
$n = 5;
$val = 20;
 
echo countSolutions($n, $val);
 
// This code is contributed by nitin mittal.
?>


Javascript


输出 :

10626