📌  相关文章
📜  递归地将数字分成 3 部分以获得最大和

📅  最后修改于: 2021-09-17 07:28:48             🧑  作者: Mango

给定一个数 n,我们只能将它分成 n/2、n/3 和 n/4 三个部分(我们将只考虑整数部分)。任务是通过递归地将数字分为三部分并将它们相加来找到我们可以得出的最大和。
例子:

Input : n = 12
Output : 13
// We break n = 12 in three parts {12/2, 12/3, 12/4} 
// = {6, 4, 3},  now current sum is = (6 + 4 + 3) = 13 
// again we break 6 = {6/2, 6/3, 6/4} = {3, 2, 1} = 3 + 
// 2 + 1 = 6 and further breaking 3, 2 and 1 we get maximum
// summation as 1, so breaking 6 in three parts produces
// maximum sum 6 only similarly breaking 4 in three   
// parts we can get maximum sum 4 and same for 3 also.
// Thus maximum sum by breaking number in parts  is=13

Input : n = 24
Output : 27
// We break n = 24 in three parts {24/2, 24/3, 24/4} 
// = {12, 8, 6},  now current sum is = (12 + 8 + 6) = 26
// As seen in example, recursively breaking 12 would
// produce value 13. So our maximum sum is 13 + 8 + 6 = 27.
// Note that recursively breaking 8 and 6 doesn't produce
// more values, that is why they are not broken further.

Input : n = 23
Output : 23
// we break n = 23 in three parts {23/2, 23/3, 23/4} = 
// {10, 7, 5}, now current sum is = (10 + 7 + 5) = 22. 
// Since  after further breaking we can not get maximum
// sum hence number is itself maximum i.e; answer is 23

此问题的一个简单解决方案递归执行。在每次调用中,我们只需要检查max((max(n/2) + max(n/3) + max(n/4)), n)并返回它。因为要么我们可以通过将数字分成几部分来获得最大总和,要么数字本身就是最大值。下面是递归算法的实现。

C++
// A simple recursive C++ program to find
// maximum sum by recursively breaking a
// number in 3 parts.
#include
using namespace std;
 
// Function to find the maximum sum
int breakSum(int n)
{
    // base conditions
    if (n==0 || n == 1)
        return n;
 
    // recursively break the number and return
    // what maximum you can get
    return max((breakSum(n/2) + breakSum(n/3) +
                breakSum(n/4)),  n);
}
 
// Driver program to run the case
int main()
{
    int n = 12;
    cout << breakSum(n);
    return 0;
}


Java
// A simple recursive JAVA program to find
// maximum sum by recursively breaking a
// number in 3 parts.
import java.io.*;
 
class GFG {
    
    // Function to find the maximum sum
    static int breakSum(int n)
    {
        // base conditions
        if (n==0 || n == 1)
            return n;
  
        // recursively break the number and return
        // what maximum you can get
        return Math.max((breakSum(n/2) + breakSum(n/3) +
                    breakSum(n/4)),  n);   
    }
         
    // Driver program to test the above function
    public static void main (String[] args) {
        int n = 12;
        System.out.println(breakSum(n));
    }
}
// This code is contributed by Amit Kumar


Python3
# A simple recursive Python3 program to
# find maximum sum by recursively breaking 
# a number in 3 parts.
 
# Function to find the maximum sum
def breakSum(n) :
 
    # base conditions
    if (n == 0 or n == 1) :
        return n
 
    # recursively break the number and
    # return what maximum you can get
    return max((breakSum(n // 2) +
                breakSum(n // 3) +
                breakSum(n // 4)), n)
 
# Driver Code
if __name__ == "__main__" :
 
    n = 12
    print(breakSum(n))
 
# This code is contributed by Ryuga


C#
// A simple recursive C# program to find
// maximum sum by recursively breaking a
// number in 3 parts.
using System;
 
class GFG {
     
    // Function to find the maximum sum
    static int breakSum(int n)
    {
        // base conditions
        if (n==0 || n == 1)
            return n;
 
        // recursively break the number
        // and return what maximum you
        // can get
        return Math.Max((breakSum(n/2)
                       + breakSum(n/3)
                 + breakSum(n/4)), n);
    }
         
    // Driver program to test the
    // above function
    public static void Main ()
    {
        int n = 12;
         
        Console.WriteLine(breakSum(n));
    }
}
 
// This code is contributed by anuj_67.


PHP


Javascript


C++
// A Dynamic programming based C++ program
// to find maximum sum by recursively breaking
// a number in 3 parts.
#include
#define MAX 1000000
using namespace std;
 
int breakSum(int n)
{
    int dp[n+1];
 
    // base conditions
    dp[0] = 0, dp[1] = 1;
 
    // Fill in bottom-up manner using recursive
    // formula.
    for (int i=2; i<=n; i++)
       dp[i] = max(dp[i/2] + dp[i/3] + dp[i/4], i);
 
    return dp[n];
}
 
// Driver program to run the case
int main()
{
    int n = 24;
    cout << breakSum(n);
    return 0;
}


Java
// A simple recursive JAVA program to find
// maximum sum by recursively breaking a
// number in 3 parts.
import java.io.*;
 
class GFG {
 
    final int MAX = 1000000;
     
    // Function to find the maximum sum
    static int breakSum(int n)
    {
        int dp[] = new int[n+1];
  
        // base conditions
        dp[0] = 0;  dp[1] = 1;
      
        // Fill in bottom-up manner using recursive
        // formula.
        for (int i=2; i<=n; i++)
           dp[i] = Math.max(dp[i/2] + dp[i/3] + dp[i/4], i);
      
        return dp[n];
    }
         
    // Driver program to test the above function
    public static void main (String[] args) {
        int n = 24;
        System.out.println(breakSum(n));
    }
}
// This code is contributed by Amit Kumar


Python3
# A Dynamic programming based Python program
# to find maximum sum by recursively breaking
# a number in 3 parts.
 
MAX = 1000000
 
def breakSum(n):
 
    dp = [0]*(n+1)
 
    # base conditions
    dp[0] = 0
    dp[1] = 1
 
    # Fill in bottom-up manner using recursive
    # formula.
    for i in range(2, n+1):
        dp[i] = max(dp[int(i/2)] + dp[int(i/3)] + dp[int(i/4)], i);
 
    return dp[n]
 
 
# Driver program to run the case
n = 24
print(breakSum(n))
 
# This code is contributed by
# Smitha Dinesh Semwal


C#
// A simple recursive C# program to find
// maximum sum by recursively breaking a
// number in 3 parts.
using System;
 
class GFG {
     
    // Function to find the maximum sum
    static int breakSum(int n)
    {
        int []dp = new int[n+1];
 
        // base conditions
        dp[0] = 0; dp[1] = 1;
     
        // Fill in bottom-up manner
        // using recursive formula.
        for (int i = 2; i <= n; i++)
            dp[i] = Math.Max(dp[i/2] +
                  dp[i/3] + dp[i/4], i);
     
        return dp[n];
    }
         
    // Driver program to test the
    // above function
    public static void Main ()
    {
        int n = 24;
        Console.WriteLine(breakSum(n));
    }
}
 
// This code is contributed by anuj_67.


PHP


Javascript


输出 :

13

这个问题的一个有效解决方案是使用动态规划,因为在递归地分解数字时,我们必须执行一些重叠的问题。例如,n = 30 的一部分将是 {15,10,7},而 15 的一部分将是 {7,5,3},因此我们必须将 7 的过程重复两次以进一步打破。为了避免这个重叠问题,我们使用动态规划。我们将值存储在一个数组中,如果递归调用中有任何数字,我们目前已经解决了该数字,因此我们直接从数组中提取它。

C++

// A Dynamic programming based C++ program
// to find maximum sum by recursively breaking
// a number in 3 parts.
#include
#define MAX 1000000
using namespace std;
 
int breakSum(int n)
{
    int dp[n+1];
 
    // base conditions
    dp[0] = 0, dp[1] = 1;
 
    // Fill in bottom-up manner using recursive
    // formula.
    for (int i=2; i<=n; i++)
       dp[i] = max(dp[i/2] + dp[i/3] + dp[i/4], i);
 
    return dp[n];
}
 
// Driver program to run the case
int main()
{
    int n = 24;
    cout << breakSum(n);
    return 0;
}

Java

// A simple recursive JAVA program to find
// maximum sum by recursively breaking a
// number in 3 parts.
import java.io.*;
 
class GFG {
 
    final int MAX = 1000000;
     
    // Function to find the maximum sum
    static int breakSum(int n)
    {
        int dp[] = new int[n+1];
  
        // base conditions
        dp[0] = 0;  dp[1] = 1;
      
        // Fill in bottom-up manner using recursive
        // formula.
        for (int i=2; i<=n; i++)
           dp[i] = Math.max(dp[i/2] + dp[i/3] + dp[i/4], i);
      
        return dp[n];
    }
         
    // Driver program to test the above function
    public static void main (String[] args) {
        int n = 24;
        System.out.println(breakSum(n));
    }
}
// This code is contributed by Amit Kumar

蟒蛇3

# A Dynamic programming based Python program
# to find maximum sum by recursively breaking
# a number in 3 parts.
 
MAX = 1000000
 
def breakSum(n):
 
    dp = [0]*(n+1)
 
    # base conditions
    dp[0] = 0
    dp[1] = 1
 
    # Fill in bottom-up manner using recursive
    # formula.
    for i in range(2, n+1):
        dp[i] = max(dp[int(i/2)] + dp[int(i/3)] + dp[int(i/4)], i);
 
    return dp[n]
 
 
# Driver program to run the case
n = 24
print(breakSum(n))
 
# This code is contributed by
# Smitha Dinesh Semwal

C#

// A simple recursive C# program to find
// maximum sum by recursively breaking a
// number in 3 parts.
using System;
 
class GFG {
     
    // Function to find the maximum sum
    static int breakSum(int n)
    {
        int []dp = new int[n+1];
 
        // base conditions
        dp[0] = 0; dp[1] = 1;
     
        // Fill in bottom-up manner
        // using recursive formula.
        for (int i = 2; i <= n; i++)
            dp[i] = Math.Max(dp[i/2] +
                  dp[i/3] + dp[i/4], i);
     
        return dp[n];
    }
         
    // Driver program to test the
    // above function
    public static void Main ()
    {
        int n = 24;
        Console.WriteLine(breakSum(n));
    }
}
 
// This code is contributed by anuj_67.

PHP


Javascript


输出:

27

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