给定一个正圆柱体,它内接在一个高度为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) 。
我们使用相似比的方法来找到高度和半径、 hy和x之间的关系。
y/x = h/r
y = hx/r
将y方程代入体积方程 V。
V = πx^2(h-y)
V = πx^2(h-hx/r)
V = πx^2h – πx^3h/r
now, dV/dx = d(πx^2h – πx^3h/r)/dx
and setting dV/dx = 0
we get, x = 0, 2r/3
So, x = 2r/3
and, y = 2h/3
So, V = π8r^2h/27
下面是上述方法的实现:
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 现场工作专业课程和学生竞争性编程现场课程。