📜  立方体中最大的右圆柱

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

给定一个边长a的立方体。任务是找到可以在其中刻出的最大右圆柱的体积。
例子:

Input :  a = 4
Output : 50.24

Input : a = 5
Output : 98.125

方法
让:

  • 圆柱体的高度为h
  • 圆柱半径为r

从图中可以清楚地看出:

  • 圆柱体的高度=立方体的侧面
  • 圆柱体的半径=立方体的侧面/ 2

所以,

h = a
r = a/2

下面是上述方法的实现:

C++
// C++ Program to find the biggest right
// circular cylinder that can be fit within a cube
#include 
using namespace std;
 
// Function to find the biggest right circular cylinder
float findVolume(float a)
{
    // side cannot be negative
    if (a < 0)
        return -1;
 
    // radius of right circular cylinder
    float r = a / 2;
 
    // height of right circular cylinder
    float h = a;
 
    // volume of right circular cylinder
    float V = 3.14 * pow(r, 2) * h;
 
    return V;
}
 
// Driver code
int main()
{
    float a = 5;
 
    cout << findVolume(a) << endl;
 
    return 0;
}


Java
// Java Program to find the biggest right
// circular cylinder that can be fit within a cube
 
import java.io.*;
 
class GFG {
   
 
// Function to find the biggest right circular cylinder
 static float findVolume(float a)
{
    // side cannot be negative
    if (a < 0)
        return -1;
 
    // radius of right circular cylinder
    float r = a / 2;
 
    // height of right circular cylinder
    float h = a;
 
    // volume of right circular cylinder
    float V = (float)(3.14 * Math.pow(r, 2) * h);
 
    return V;
}
 
// Driver code
 
 
    public static void main (String[] args) {
            float a = 5;
 
    System.out.print(findVolume(a));
    }
}
// This code is contributed by anuj_67..


Python3
# Python3 Program to find the biggest
# right circular cylinder that can be
# fit within a cube
 
# Function to find the biggest right
# circular cylinder
def findVolume(a) :
 
    # side cannot be negative
    if (a < 0) :
        return -1
 
    # radius of right circular cylinder
    r = a / 2
 
    # height of right circular cylinder
    h = a
 
    # volume of right circular cylinder
    V = 3.14 * pow(r, 2) * h
 
    return V
 
# Driver code
if __name__ == "__main__" :
 
    a = 5
 
    print(findVolume(a))
 
# This code is contributed by Ryuga


C#
// C# Program to find the biggest right
// circular cylinder that can be fit within a cube
 
using System;
class GFG {
 
 
// Function to find the biggest right circular cylinder
static float findVolume(float a)
{
    // side cannot be negative
    if (a < 0)
        return -1;
 
    // radius of right circular cylinder
    float r = a / 2;
 
    // height of right circular cylinder
    float h = a;
 
    // volume of right circular cylinder
    float V = (float)(3.14 * Math.Pow(r, 2) * h);
 
    return V;
}
 
// Driver code
 
 
    public static void Main () {
            float a = 5;
 
   Console.WriteLine(findVolume(a));
    }
}
// This code is contributed by anuj_67..


PHP


Javascript


输出:
98.125