Dado que aquí hay un cubo de longitud de lado a , la tarea es encontrar la esfera más grande que se puede inscribir dentro de él.
Ejemplos:
Input: a = 4 Output: 2 Input: a = 5 Output: 2.5
Enfoque :
Del diagrama 2d está claro que, 2r = a ,
donde, a = lado del cubo
r = radio de la esfera
entonces r = a/2 .
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ Program to find the biggest sphere // inscribed within a cube #include <bits/stdc++.h> 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
<?php // PHP Program to find the biggest // sphere inscribed within a cube // Function to find the radius // of the sphere function sphere($a) { // side cannot be negative if ($a < 0) return -1; // radius of the sphere $r = ($a / 2); return $r; } // Driver code $a = 5; echo sphere($a); // This code is contributed by akt_mit ?>
Javascript
<script> // javascript Program to find the biggest sphere // inscribed within a cube // Function to find the radius of the sphere function sphere(a) { // side cannot be negative if (a < 0) return -1; // radius of the sphere var r = a / 2; return r; } // Driver code var a = 5; document.write(sphere(a)); // This code is contributed by 29AjayKumar </script>
Producción:
2.5
Complejidad de tiempo: O(1)
Espacio Auxiliar: O(1)
Publicación traducida automáticamente
Artículo escrito por IshwarGupta y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA