📜  最大值,可以选择除以或原样考虑

📅  最后修改于: 2021-04-29 11:12:03             🧑  作者: Mango

给定一个数字n,我们需要借助以下函数来找到最大和:
F(n)= max((F(n / 2)+ F(n / 3)+ F(n / 4)+ F(n / 5)),n) 。为了计算F(n,),我们可以将n作为结果,也可以按照给定的函数定义将n进一步分为四部分。这可以尽我们所能完成。找到可以从给定的N获得的最大可能总和。注意:1不能进一步分解,因此F(1)= 1作为基本情况。

例子 :

Input : n = 10
Output : MaxSum = 12
Explanation: 
f(10) = f(10/2) + f(10/3) + f(10/4) + f(10/5)
      = f(5)  +   f(3)  +  f(2)   +  f(2)
      = 12
5, 3 and 2 cannot be further divided.

Input : n = 2
Output : MaxSum = 2

方法:可以使用递归方法解决此问题,但是由于子问题重叠,将使我们付出很高的复杂性。因此,我们采用动态编程概念以自下而上的方式解决此问题,如下所示:

C++
// CPP program for maximize result when
// we have choice to divide or consider
// as it is.
#include 
using namespace std;
 
// function for calculating max possible result
int maxDP(int n)
{
    int res[n + 1];
    res[0] = 0;
    res[1] = 1;
 
    // Compute remaining values in bottom
    // up manner.
    for (int i = 2; i <= n; i++)
        res[i] = max(i, (res[i / 2]
                         + res[i / 3]
                         + res[i / 4]
                         + res[i / 5]));
 
    return res[n];
}
 
// Driver Code
int main()
{
    int n = 60;
    cout << "MaxSum =" << maxDP(n);
    return 0;
}


Java
// Java program for maximize result when
// we have choice to divide or consider
// as it is.
import java.io.*;
 
class GFG {
 
    // function for calculating max
    // possible result
    static int maxDP(int n)
    {
        int res[] = new int[n + 1];
        res[0] = 0;
        res[1] = 1;
 
        // Compute remaining values in
        // bottom up manner.
        for (int i = 2; i <= n; i++)
            res[i]
                = Math.max(i, (res[i / 2]
                               + res[i / 3]
                               + res[i / 4]
                               + res[i / 5]));
 
        return res[n];
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int n = 60;
        System.out.println("MaxSum = " + maxDP(n));
    }
}
 
// This code is contributed by vt_m


Python3
# Python3 code to maximize result when
# we have choice to divide or consider
# as it is.
 
# function for calculating max
# possible result
def maxDP (n):
    res = list()
    res.append(0)
    res.append(1)
     
    # Compute remaining values in
    # bottom up manner.
    i = 2
    while i


C#
// C# program for maximize result when
// we have choice to divide or consider
// as it is.
using System;
 
class GFG {
 
    // function for calculating max
    // possible result
    static int maxDP(int n)
    {
        int[] res = new int[n + 1];
        res[0] = 0;
        res[1] = 1;
 
        // Compute remaining values in
        // bottom up manner.
        for (int i = 2; i <= n; i++)
            res[i] = Math.Max(i, (res[i / 2]
                                  + res[i / 3]
                                  + res[i / 4]
                                  + res[i / 5]));
 
        return res[n];
    }
 
    // Driver code
    public static void Main()
    {
        int n = 60;
        Console.WriteLine("MaxSum = " + maxDP(n));
    }
}
 
// This code is contributed by vt_m


PHP


Javascript


输出
MaxSum =106

时间复杂度: O(N)
辅助空间: O(N)