📜  从给定的周长和面积中找到长方体的最大体积

📅  最后修改于: 2021-04-24 21:34:01             🧑  作者: Mango

给定周长P和面积A,任务是从给定的周长和表面积计算以长方体形式可以制成的最大体积。

例子 :

Input: P = 24, A = 24
Output: 8

Input: P = 20, A = 14
Outpu: 3

方法:对于给定的长方体周长,我们有P = 4(l + b + h)-(i),
对于给定的长方体面积,我们有A = 2(lb + bh + lh)-(ii)。
长方体的体积为V = lbh
音量取决于3个变量l,b,h。让它仅取决于长度。

下面是上述方法的实现:

C++
// C++ implementation of the above approach
#include 
using namespace std;
 
// function to return maximum volume
float maxVol(float P, float A)
{
    // calculate length
    float l = (P - sqrt(P * P - 24 * A)) / 12;
 
    // calculate volume
    float V = l * (A / 2.0 - l * (P / 4.0 - l));
 
    // return result
    return V;
}
 
// Driver code
int main()
{
    float P = 20, A = 16;
   
    // Function call
    cout << maxVol(P, A);
 
    return 0;
}


Java
// Java implementation of the above approach
import java.util.*;
 
class Geeks {
 
    // function to return maximum volume
    static float maxVol(float P, float A)
    {
        // calculate length
        float l
            = (float)(P - Math.sqrt(P * P - 24 * A)) / 12;
 
        // calculate volume
        float V
            = (float)(l * (A / 2.0 - l * (P / 4.0 - l)));
 
        // return result
        return V;
    }
 
    // Driver code
    public static void main(String args[])
    {
        float P = 20, A = 16;
       
        // Function call
        System.out.println(maxVol(P, A));
    }
}
 
// This code is contributed by Kirti_Mangal


Python3
# Python3 implementation of the
# above approach
from math import sqrt
 
# function to return maximum volume
 
 
def maxVol(P, A):
 
    # calculate length
    l = (P - sqrt(P * P - 24 * A)) / 12
 
    # calculate volume
    V = l * (A / 2.0 - l * (P / 4.0 - l))
 
    # return result
    return V
 
 
# Driver code
if __name__ == '__main__':
    P = 20
    A = 16
     
    # Function call
    print(maxVol(P, A))
 
# This code is contributed
# by Surendra_Gangwar


C#
// C# implementation of the above approach
using System;
 
class GFG {
 
    // function to return maximum volume
    static float maxVol(float P, float A)
    {
        // calculate length
        float l
            = (float)(P - Math.Sqrt(P * P - 24 * A)) / 12;
 
        // calculate volume
        float V
            = (float)(l * (A / 2.0 - l * (P / 4.0 - l)));
 
        // return result
        return V;
    }
 
    // Driver code
    public static void Main()
    {
        float P = 20, A = 16;
        
        // Function call
        Console.WriteLine(maxVol(P, A));
    }
}
 
// This code is contributed
// by Akanksha Rai


PHP


Javascript


输出
4.14815