📌  相关文章
📜  在给定的边数总和下最大化长方体的体积

📅  最后修改于: 2021-04-29 18:20:58             🧑  作者: Mango

我们得到了长方体的长度,宽度高度的总和,即S。任务是找到可以达到的最大体积,以使边的总和为S。
长方体的体积=长*宽*高
例子 :

Input : s  = 4
Output : 2
Only possible dimensions are some combination of 1, 1, 2.

Input : s = 8
Output : 18
All possible edge dimensions:
[1, 1, 6], volume = 6
[1, 2, 5], volume = 10
[1, 3, 4], volume = 12
[2, 2, 4], volume = 16
[2, 3, 3], volume = 18

方法1 :(强力)
运行三个嵌套的想法,一个嵌套的长度,一个嵌套的宽度,一个嵌套的高度。对于每次迭代,请计算音量并将其与最大音量进行比较。
以下是此方法的实现:

C++
#include 
using namespace std;
 
// Return the maximum volume.
int maxvolume(int s)
{
    int maxvalue = 0;
 
    // for length
    for (int i = 1; i <= s - 2; i++) {
 
        // for breadth
        for (int j = 1; j <= s - 1; j++) {
 
            // for height
            int k = s - i - j;
 
            // calculating maximum volume.
            maxvalue = max(maxvalue, i * j * k);
        }
    }
 
    return maxvalue;
}
 
// Driven Program
int main()
{
    int s = 8;
    cout << maxvolume(s) << endl;
    return 0;
}


Java
// Java code to Maximize volume of
// cuboid with given sum of sides
 
class GFG
{
     
    // Return the maximum volume.
    static int maxvolume(int s)
    {
        int maxvalue = 0;
     
        // for length
        for (int i = 1; i <= s - 2; i++)
        {
     
            // for breadth
            for (int j = 1; j <= s - 1; j++)
            {
     
                // for height
                int k = s - i - j;
     
                // calculating maximum volume.
                maxvalue = Math.max(maxvalue, i * j * k);
            }
        }
     
        return maxvalue;
    }
    // Driver function
    public static void main (String[] args)
    {
        int s = 8;
        System.out.println(maxvolume(s));
    }
}
 
// This code is contributed by Anant Agarwal.


Python3
# Python3 code to Maximize volume of
# cuboid with given sum of sides
 
# Return the maximum volume.
def maxvolume (s):
    maxvalue = 0
 
    # for length
    i = 1
    for i in range(s - 1):
        j = 1
         
        # for breadth
        for j in range(s):
             
            # for height
            k = s - i - j
             
            # calculating maximum volume.
            maxvalue = max(maxvalue, i * j * k)
             
    return maxvalue
     
# Driven Program
s = 8
print(maxvolume(s))
 
# This code is contributed by "Sharad_Bhardwaj".


C#
// C# code to Maximize volume of
// cuboid with given sum of sides
using System;
 
class GFG
{
     
    // Return the maximum volume.
    static int maxvolume(int s)
    {
        int maxvalue = 0;
     
        // for length
        for (int i = 1; i <= s - 2; i++)
        {
     
            // for breadth
            for (int j = 1; j <= s - 1; j++)
            {
     
                // for height
                int k = s - i - j;
     
                // calculating maximum volume.
                maxvalue = Math.Max(maxvalue, i * j * k);
            }
        }
     
        return maxvalue;
    }
     
     
    // Driver function
    public static void Main ()
    {
        int s = 8;
        Console.WriteLine(maxvolume(s));
    }
}
 
// This code is contributed by vt_m.


PHP


Javascript


C++
#include 
using namespace std;
 
// Return the maximum volume.
int maxvolume(int s)
{
    // finding length
    int length = s / 3;
 
    s -= length;
 
    // finding breadth
    int breadth = s / 2;
 
    // finding height
    int height = s - breadth;
 
    return length * breadth * height;
}
 
// Driven Program
int main()
{
    int s = 8;
    cout << maxvolume(s) << endl;
    return 0;
}


Java
// Java code to Maximize volume of
// cuboid with given sum of sides
import java.io.*;
 
class GFG
{
    // Return the maximum volume.
    static int maxvolume(int s)
    {
        // finding length
        int length = s / 3;
     
        s -= length;
     
        // finding breadth
        int breadth = s / 2;
     
        // finding height
        int height = s - breadth;
     
        return length * breadth * height;
    }
     
    // Driven Program
    public static void main (String[] args)
    {
        int s = 8;
        System.out.println ( maxvolume(s));
                 
    }
}
 
// This code is contributed by vt_m.


Python3
# Python3 code to Maximize volume of
# cuboid with given sum of sides
 
# Return the maximum volume.
def maxvolume( s ):
 
    # finding length
    length = int(s / 3)
     
    s -= length
     
    # finding breadth
    breadth = s / 2
     
    # finding height
    height = s - breadth
     
    return int(length * breadth * height)
     
# Driven Program
s = 8
print( maxvolume(s) )
 
# This code is contributed by "Sharad_Bhardwaj".


C#
// C# code to Maximize volume of
// cuboid with given sum of sides
using System;
 
class GFG
{
    // Return the maximum volume.
    static int maxvolume(int s)
    {
        // finding length
        int length = s / 3;
     
        s -= length;
     
        // finding breadth
        int breadth = s / 2;
     
        // finding height
        int height = s - breadth;
     
        return length * breadth * height;
    }
     
    // Driven Program
    public static void Main ()
    {
        int s = 8;
        Console.WriteLine( maxvolume(s));
                 
    }
}
 
// This code is contributed by vt_m.


PHP


Javascript


输出 :

18

时间复杂度: O(n 2 )方法2 :(有效方法)
想法是尽可能平均地划分边缘。
所以,
长度=地板(s / 3)
宽度=地板((s-长度)/ 2)=地板((s-地板(s / 3)/ 2)
高度= s –长度–宽度= s –地板(s / 3)–地板((s –地板(s / 3))/ 2)
以下是此方法的实现:

C++

#include 
using namespace std;
 
// Return the maximum volume.
int maxvolume(int s)
{
    // finding length
    int length = s / 3;
 
    s -= length;
 
    // finding breadth
    int breadth = s / 2;
 
    // finding height
    int height = s - breadth;
 
    return length * breadth * height;
}
 
// Driven Program
int main()
{
    int s = 8;
    cout << maxvolume(s) << endl;
    return 0;
}

Java

// Java code to Maximize volume of
// cuboid with given sum of sides
import java.io.*;
 
class GFG
{
    // Return the maximum volume.
    static int maxvolume(int s)
    {
        // finding length
        int length = s / 3;
     
        s -= length;
     
        // finding breadth
        int breadth = s / 2;
     
        // finding height
        int height = s - breadth;
     
        return length * breadth * height;
    }
     
    // Driven Program
    public static void main (String[] args)
    {
        int s = 8;
        System.out.println ( maxvolume(s));
                 
    }
}
 
// This code is contributed by vt_m.

Python3

# Python3 code to Maximize volume of
# cuboid with given sum of sides
 
# Return the maximum volume.
def maxvolume( s ):
 
    # finding length
    length = int(s / 3)
     
    s -= length
     
    # finding breadth
    breadth = s / 2
     
    # finding height
    height = s - breadth
     
    return int(length * breadth * height)
     
# Driven Program
s = 8
print( maxvolume(s) )
 
# This code is contributed by "Sharad_Bhardwaj".

C#

// C# code to Maximize volume of
// cuboid with given sum of sides
using System;
 
class GFG
{
    // Return the maximum volume.
    static int maxvolume(int s)
    {
        // finding length
        int length = s / 3;
     
        s -= length;
     
        // finding breadth
        int breadth = s / 2;
     
        // finding height
        int height = s - breadth;
     
        return length * breadth * height;
    }
     
    // Driven Program
    public static void Main ()
    {
        int s = 8;
        Console.WriteLine( maxvolume(s));
                 
    }
}
 
// This code is contributed by vt_m.

的PHP


Java脚本


输出 :

18

时间复杂度: O(1)
这是如何运作的?