在给定边和的情况下最大化长方体的体积

我们得到了 长度、宽度 身高 比如说S,指的是长方体。这项任务是找到可以达到的最大体积,这样边的和就是S。 长方体的体积=长度*宽度*高度 例如:

null
Input : s  = 4Output : 2Only possible dimensions are some combination of 1, 1, 2.Input : s = 8Output : 18All 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 <bits/stdc++.h>
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

<?php
// PHP code to Maximize volume of
// cuboid with given sum of sides
// Return the maximum volume.
function maxvolume( $s )
{
$maxvalue = 0;
// for length
for ( $i = 1; $i <= $s - 2; $i ++)
{
// for breadth
for ( $j = 1; $j <= $s - 1; $j ++)
{
// for height
$k = $s - $i - $j ;
// calculating maximum volume.
$maxvalue = max( $maxvalue ,
$i * $j * $k );
}
}
return $maxvalue ;
}
// Driver Code
$s = 8;
echo (maxvolume( $s ));
// This code is contributed by vt_m.
?>


Javascript

<script>
// javascript code to Maximize volume of
// cuboid with given sum of sides
// Return the maximum volume.
function maxvolume( s)
{
let maxvalue = 0;
// for length
for (let i = 1; i <= s - 2; i++)
{
// for breadth
for (let j = 1; j <= s - 1; j++)
{
// for height
let k = s - i - j;
// calculating maximum volume.
maxvalue = Math.max(maxvalue, i * j * k);
}
}
return maxvalue;
}
// Driver code
let s = 8;
document.write(maxvolume(s));
// This code is contributed by gauravrajput1
</script>


输出:

18

时间复杂性: O(n) 2. ) 方法2:(有效方法) 这样做的目的是尽可能平均地分割边缘。 所以 长度=楼层(s/3) 宽度=楼层((s-长度)/2)=楼层((s-楼层(s/3)/2) 高度=s–长度–宽度=s–楼层(s/3)-楼层(s–楼层(s/3))/2) 以下是该方法的实施情况:

C++

#include <bits/stdc++.h>
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

<?php
// Return the maximum volume.
function maxvolume( $s )
{
// finding length
$length = (int)( $s / 3);
$s -= $length ;
// finding breadth
$breadth = (int)( $s / 2);
// finding height
$height = $s - $breadth ;
return $length * $breadth * $height ;
}
// Driven Code
$s = 8;
echo (maxvolume( $s ));
// This code is contributed by Ajit.
?>


Javascript

<script>
// Return the maximum volume.
function maxvolume( s)
{
// finding length
let length = parseInt(s / 3);
s -= length;
// finding breadth
let breadth = parseInt(s / 2);
// finding height
let height = s - breadth;
return length * breadth * height;
}
// Driven Program
let s = 8;
document.write(maxvolume(s));
// This code is contributed by aashish1995
</script>


输出:

18

时间复杂性: O(1) 这是怎么回事?

我们基本上需要最大化 三个数字,x,y和z,其和是给定的。 给定s=x+y+z 最大化P=x*y*z =x*y*(s-x-y) =x*y*s–x*x*s–x*y*y 我们得到dp/dx=sy-2xy-y*y dp/dy=sx-2xy-x*x 我们得到dp/dx=0和dp/dy=0 x=s/3,y=s/3 所以z=s-x-y=s/3

© 版权声明
THE END
喜欢就支持一下吧
点赞7 分享