这里给出的是边长为a的立方体,任务是找到可以在其内切的最大领域。
例子:
Input: a = 4
Output: 2
Input: a = 5
Output: 2.5
方法:
From the 2d diagram it is clear that, 2r = a,
where, a = side of the cube
r = radius of the sphere
so r = a/2.
下面是上述方法的实现:
C++
// C++ Program to find the biggest sphere
// inscribed within a cube
#include
using namespace std;
// Function to find the radius of the sphere
float sphere(float a)
{
// side cannot be negative
if (a < 0)
return -1;
// radius of the sphere
float r = a / 2;
return r;
}
// Driver code
int main()
{
float a = 5;
cout << sphere(a) << endl;
return 0;
}
Java
// Java Program to find the biggest sphere
// inscribed within a cube
class GFG{
// Function to find the radius of the sphere
static float sphere(float a)
{
// side cannot be negative
if (a < 0)
return -1;
// radius of the sphere
float r = a / 2;
return r;
}
// Driver code
public static void main(String[] args)
{
float a = 5;
System.out.println(sphere(a));
}
}
// This code is contributed by mits
Python3
# Python 3 Program to find the biggest
# sphere inscribed within a cube
# Function to find the radius
# of the sphere
def sphere(a):
# side cannot be negative
if (a < 0):
return -1
# radius of the sphere
r = a / 2
return r
# Driver code
if __name__ == '__main__':
a = 5
print(sphere(a))
# This code is contributed
# by SURENDRA_GANGWAR
C#
// C# Program to find the biggest
// sphere inscribed within a cube
using System;
class GFG
{
// Function to find the radius
// of the sphere
static float sphere(float a)
{
// side cannot be negative
if (a < 0)
return -1;
// radius of the sphere
float r = a / 2;
return r;
}
// Driver code
static public void Main ()
{
float a = 5;
Console.WriteLine(sphere(a));
}
}
// This code is contributed by ajit
PHP
Javascript
输出:
2.5
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。