📜  可以刻在圆锥内的最大右圆柱

📅  最后修改于: 2021-05-07 01:21:19             🧑  作者: 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