这里给出的是一个高度为h 、顶半径为r和底半径为R的截锥体,它内接一个直圆柱体,该圆柱体又内接一个球体。任务是找到这个球体的最大可能体积。
例子:
Input: r = 5, R = 8, h = 11
Output: 523.333
Input: r = 9, R = 14, h = 20
Output:3052.08
方法:让圆柱体的高度 = H ,球体的半径 = x
我们知道,截锥体内接圆柱体的高度和半径分别等于截头体的高度和顶部半径(请参考这里)。所以圆柱体的高度= h ,圆柱体的半径= r 。
此外,内接圆柱体的球体半径等于圆柱体的半径(请参阅此处),因此x = r 。
因此,球体的体积V = 4*π*r^3/3 。
下面是上述方法的实现:
C++
// C++ Program to find the biggest sphere
// that can be inscribed within a right
// circular cylinder which in turn is inscribed
// within a frustum
#include
using namespace std;
// Function to find the biggest sphere
float sph(float r, float R, float h)
{
// the radii and height cannot be negative
if (r < 0 && R < 0 && h < 0)
return -1;
// radius of the sphere
float x = r;
// volume of the sphere
float V = (4 * 3.14 * pow(r, 3)) / 3;
return V;
}
// Driver code
int main()
{
float r = 5, R = 8, h = 11;
cout << sph(r, R, h) << endl;
return 0;
}
Java
// Java Program to find the biggest sphere
// that can be inscribed within a right
// circular cylinder which in turn is inscribed
// within a frustum
import java.lang.Math;
class gfg
{
// Function to find the biggest sphere
static float sph(float r, float R, float h)
{
// the radii and height cannot be negative
if (r < 0 && R < 0 && h < 0)
return -1;
// radius of the sphere
float x = r;
// volume of the sphere
float V = (float)(4 * 3.14f * Math.pow(r, 3)) / 3;
return V;
}
// Driver code
public static void main(String[] args)
{
float r = 5, R = 8, h = 11;
System.out.println(sph(r, R, h));
}
}
// This Code is contributed by Code_Mech.
Python3
# Python3 Program to find the biggest sphere
# that can be inscribed within a right
# circular cylinder which in turn is inscribed
# within a frustum
import math as mt
# Function to find the biggest sphere
def sph(r, R, h):
# the radii and height cannot
# be negative
if (r < 0 and R < 0 and h < 0):
return -1
# radius of the sphere
x = r
# volume of the sphere
V = (4 * 3.14 * pow(r, 3)) / 3
return V
# Driver code
r, R, h = 5, 8, 11
print(sph(r, R, h))
# This code is contributed by
# Mohit kumar 29
C#
// C# Program to find the biggest sphere
// that can be inscribed within a right
// circular cylinder which in turn is
// inscribed within a frustum
using System;
class gfg
{
// Function to find the biggest sphere
static float sph(float r, float R, float h)
{
// the radii and height
// cannot be negative
if (r < 0 && R < 0 && h < 0)
return -1;
// radius of the sphere
float x = r;
// volume of the sphere
float V = (float)(4 * 3.14f *
Math.Pow(r, 3)) / 3;
return V;
}
// Driver code
public static void Main()
{
float r = 5, R = 8, h = 11;
Console.WriteLine(sph(r, R, h));
}
}
// This code is contributed by Ryuga
PHP
Javascript
输出:
523.333