📜  使用大于或等于 m 的数字对 n 求和的不同方法

📅  最后修改于: 2021-09-22 09:44:50             🧑  作者: Mango

给定两个自然数nm 。任务是找到可以将大于或等于 m 的数字相加以获得总和 n 的方法数。
例子:

Input : n = 3, m = 1
Output : 3
Following are three different ways
to get sum n such that each term is
greater than or equal to m
1 + 1 + 1, 1 + 2, 3 

Input : n = 2, m = 1
Output : 2
Two ways are 1 + 1 and 2

这个想法是通过定义二维矩阵来使用动态规划,比如 dp[][]。 dp[i][j]定义使用大于或等于 j 的数字来获得和 i 的方法数。所以 dp[i][j] 可以定义为:

下面是这个方法的实现:

C++
// CPP Program to find number of ways to
// which numbers that are greater than
// given number can be added to get sum.
#include 
#define MAX 100
using namespace std;
 
// Return number of ways to which numbers
// that are greater than given number can
// be added to get sum.
int numberofways(int n, int m)
{
    int dp[n+2][n+2];
    memset(dp, 0, sizeof(dp));
 
    dp[0][n + 1] = 1;
 
    // Filling the table. k is for numbers
    // greater than or equal that are allowed.
    for (int k = n; k >= m; k--) {
 
        // i is for sum
        for (int i = 0; i <= n; i++) {
 
            // initializing dp[i][k] to number
            // ways to get sum using numbers
            // greater than or equal k+1
            dp[i][k] = dp[i][k + 1];
 
            // if i > k
            if (i - k >= 0)
                dp[i][k] = (dp[i][k] + dp[i - k][k]);
        }
    }
 
    return dp[n][m];
}
 
// Driver Program
int main()
{
    int n = 3, m = 1;
    cout << numberofways(n, m) << endl;
    return 0;
}


Java
// Java Program to find number of ways to
// which numbers that are greater than
// given number can be added to get sum.
import java.io.*;
 
class GFG {
     
    // Return number of ways to which numbers
    // that are greater than given number can
    // be added to get sum.
    static int numberofways(int n, int m)
    {
        int dp[][]=new int[n+2][n+2];
         
        dp[0][n + 1] = 1;
      
        // Filling the table. k is for numbers
        // greater than or equal that are allowed.
        for (int k = n; k >= m; k--) {
      
            // i is for sum
            for (int i = 0; i <= n; i++) {
      
                // initializing dp[i][k] to number
                // ways to get sum using numbers
                // greater than or equal k+1
                dp[i][k] = dp[i][k + 1];
      
                // if i > k
                if (i - k >= 0)
                    dp[i][k] = (dp[i][k] + dp[i - k][k]);
            }
        }
      
        return dp[n][m];
    }
      
    // Driver Program
    public static void main(String args[])
    {
        int n = 3, m = 1;
        System.out.println(numberofways(n, m));
    }
}
 
/*This code is contributed by Nikita tiwari.*/


Python3
# Python3 Program to find number of ways to
# which numbers that are greater than
# given number can be added to get sum.
MAX = 100
import numpy as np
 
# Return number of ways to which numbers
# that are greater than given number can
# be added to get sum.
 
def numberofways(n, m) :
         
    dp = np.zeros((n + 2, n + 2))
     
    dp[0][n + 1] = 1
 
    # Filling the table. k is for numbers
    # greater than or equal that are allowed.
    for k in range(n, m - 1, -1) :
 
        # i is for sum
        for i in range(n + 1) :
 
            # initializing dp[i][k] to number
            # ways to get sum using numbers
            # greater than or equal k+1
            dp[i][k] = dp[i][k + 1]
 
            # if i > k
            if (i - k >= 0) :
                dp[i][k] = (dp[i][k] + dp[i - k][k])
 
    return dp[n][m]
 
# Driver Code
if __name__ == "__main__" :
 
    n, m = 3, 1
    print(numberofways(n, m))
 
# This code is contributed by Ryuga


C#
// C# program to find number of ways to
// which numbers that are greater than
// given number can be added to get sum.
using System;
 
class GFG {
 
    // Return number of ways to which numbers
    // that are greater than given number can
    // be added to get sum.
    static int numberofways(int n, int m)
    {
        int[, ] dp = new int[n + 2, n + 2];
 
        dp[0, n + 1] = 1;
 
        // Filling the table. k is for numbers
        // greater than or equal that are allowed.
        for (int k = n; k >= m; k--) {
 
            // i is for sum
            for (int i = 0; i <= n; i++) {
 
                // initializing dp[i][k] to number
                // ways to get sum using numbers
                // greater than or equal k+1
                dp[i, k] = dp[i, k + 1];
 
                // if i > k
                if (i - k >= 0)
                    dp[i, k] = (dp[i, k] + dp[i - k, k]);
            }
        }
 
        return dp[n, m];
    }
 
    // Driver Program
    public static void Main()
    {
        int n = 3, m = 1;
        Console.WriteLine(numberofways(n, m));
    }
}
 
/*This code is contributed by vt_m.*/


PHP
= $m; $k--)
    {
 
        // i is for sum
        for ($i = 0; $i <= $n; $i++)
        {
 
            // initializing dp[i][k] to number
            // ways to get sum using numbers
            // greater than or equal k+1
            $dp[$i][$k] = $dp[$i][$k + 1];
 
            // if i > k
            if ($i - $k >= 0)
                $dp[$i][$k] = ($dp[$i][$k] + $dp[$i - $k][$k]);
        }
    }
 
    return $dp[$n][$m];
}
 
    // Driver Program
    $n = 3;
    $m = 1;
    echo numberofways($n, $m) ;
    return 0;
     
    // This code is contributed by ChitraNayal
?>


Javascript


输出:

3

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程