📜  可内接在圆锥内的最大直圆柱

📅  最后修改于: 2021-10-23 08:42:29             🧑  作者: Mango

给定一个正圆柱体,它内接在一个高度为h且底半径为r的圆锥体中。任务是找到圆柱体的最大可能体积。
例子:

Input: r = 4, h = 8
Output: 119.087

Input: r = 5, h = 9
Output: 209.333

方法:圆柱体的体积为V = πr^2h
在这个问题中,首先使用相似三角形根据圆锥的高度和半径推导出体积方程。一旦我们修改了体积方程,我们将取体积的导数并求解最大值。
x为圆柱的半径, y为从圆锥顶部到内接圆柱顶部的距离。因此,圆柱体的高度为h – y
内接圆柱体的体积为V = πx^2(hy)
我们使用相似比的方法来找到高度和半径、 hyx之间的关系。
y/x = h/r
y = hx/r
y方程代入体积方程 V。

下面是上述方法的实现:

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


Java
// Java Program to find the biggest
// right circular cylinder that can
// be fit within a right circular cone
 
import java.io.*;
 
class GFG {
// Function to find the biggest right circular cylinder
static double cyl(double r, double h)
{
 
    // radius and height cannot be negative
    if (r < 0 && h < 0)
        return -1;
 
    // radius of right circular cylinder
    double R = (2 * r) / 3;
 
    // height of right circular cylinder
    double H = (2 * h) / 3;
 
    // volume of right circular cylinder
    double V = 3.14 * Math.pow(R, 2) * H;
 
    return V;
}
 
// Driver code
     
    public static void main (String[] args) {
     
    double r = 4, h = 8;
    System.out.println (cyl(r, h));
    }
//This code is contributed by ajit
}


Python 3
# Python 3 Program to find the biggest
# right circular cylinder that can
# be fit within a right circular cone
import math
 
# Function to find the biggest
# right circular cylinder
def cyl(r, h):
 
    # radius and height cannot
    # be negative
    if (r < 0 and h < 0):
        return -1
 
    # radius of right circular cylinder
    R = (2 * r) / 3
 
    # height of right circular cylinder
    H = (2 * h) / 3
     
    # volume of right circular cylinder
    V = 3.14 * math.pow(R, 2) * H
 
    return V
 
# Driver code
r = 4; h = 8;
print(cyl(r, h), "\n")
 
# This code is contributed
# by Akanksha Rai


C#
// C# Program to find the biggest
// right circular cylinder that
// can be fit within a right circular cone
using System;
 
class GFG
{
     
// Function to find the biggest
// right circular cylinder
static double cyl(double r, double h)
{
 
    // radius and height cannot
    // be negative
    if (r < 0 && h < 0)
        return -1;
 
    // radius of right circular cylinder
    double R = (2 * r) / 3;
 
    // height of right circular cylinder
    double H = (2 * h) / 3;
 
    // volume of right circular cylinder
    double V = 3.14 * Math.Pow(R, 2) * H;
 
    return V;
}
 
// Driver code
static public void Main ()
{
    double r = 4, h = 8;
    Console.WriteLine(cyl(r, h));
}
}
 
// This code is contributed by jit_t


PHP


Javascript


输出:
119.087

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程