📜  正圆柱内最大球体的体积

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

给定一个半径为正的圆柱体r  和身高h  .任务是找到可以内接的最大球体的半径。
例子

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

Input : r = 5, h= 10
Output :5

方法:从图中可以看出,球体的半径显然等于圆柱体的底半径。
所以, R = r
下面是上述方法的实现:

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


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


Python3
# Python 3 Program to find the biggest
# sphere that can be fit within a right
# circular cylinder
 
# Function to find the biggest sphere
def sph(r, h):
     
    # radius and height cannot
    # be negative
    if (r < 0 and h < 0):
        return -1
 
    # radius of sphere
    R = r
    return float(R)
 
# Driver code
r, h = 4, 8
print(sph(r, h))
 
# This code is contributed
# by PrinciRaj1992


C#
// C# Program to find the biggest
// sphere that can be fit within a
// right circular cylinder
using System;
 
class GFG
{
 
// Function to find the biggest sphere
static float sph(float r, float h)
{
 
    // radius and height cannot
    // be negative
    if (r < 0 && h < 0)
        return -1;
 
    // radius of sphere
    float R = r;
    return R;
}
 
// Driver code
public static void Main ()
{
    float r = 4, h = 8;
    Console.WriteLine(sph(r, h));
}
}
 
// This code is contributed
// by shs..


PHP


Javascript


输出:
4