Aquí se da un cubo de longitud de lado a , que inscribe un cono que a su vez inscribe un cilindro circular recto. La tarea es encontrar el mayor volumen posible de este cilindro.
Ejemplos:
Input: a = 5 Output: 232.593 Input: a = 8 Output: 952.699
Enfoque :
A partir de la figura, es muy claro, la altura del cono, H = a y el radio del cono, R = a√2 , consulte el cono más grande que se puede inscribir dentro de un cubo .
y el radio del cilindro, r = 2R/3 y la altura del cilindro, h = 2H/3 , consulte el cilindro circular recto más grande que se puede inscribir dentro de un cono .
Entonces, el radio del cilindro con respecto al cubo, r = 2a√2/3 y la altura del cilindro con respecto al cubo, h = 2a/3 .
Entonces, volumen del cilindro, V = 16πa^3/27 .
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ Program to find the biggest right circular // cylinder that can be inscribed within a right // circular cone which in turn is inscribed // within a cube #include <bits/stdc++.h> using namespace std; // Function to find the biggest // right circular cylinder float cyl(float a) { // side cannot be negative if (a < 0) return -1; // radius of right circular cylinder float r = (2 * a * sqrt(2)) / 3; // height of right circular cylinder float h = (2 * a) / 3; // volume of right circular cylinder float V = 3.14 * pow(r, 2) * h; return V; } // Driver code int main() { float a = 5; cout << cyl(a) << endl; return 0; }
Java
// Java Program to find the biggest right circular // cylinder that can be inscribed within a right // circular cone which in turn is inscribed // within a cube import java.lang.Math; class cfg { // Function to find the biggest // right circular cylinder static float cyl(float a) { // side cannot be negative if (a < 0) return -1; // radius of right circular cylinder float r = (2 * a *(float)(Math.sqrt (2)) / 3); // height of right circular cylinder float h = (2 * a) / 3; // volume of right circular cylinder float V =(3.14f *(float)(Math.pow(r, 2) * h)); return V; } // Driver code public static void main(String[] args) { float a = 5; System.out.println(cyl(a)); } } // This code is contributed by Mukul Singh.
Python3
# Python3 Program to find the biggest # right circular cylinder that can be # inscribed within a right circular # cone which in turn is inscribed # within a cube import math as mt # Function to find the biggest # right circular cylinder def cyl(a): # side cannot be negative if (a < 0): return -1 # radius of right circular cylinder r = (2 * a * mt.sqrt(2)) / 3 # height of right circular cylinder h = (2 * a) / 3 # volume of right circular cylinder V = 3.14 * pow(r, 2) * h return V # Driver code a = 5 print(cyl(a)) # This code is contributed by # Mohit kumar 29
C#
// C# Program to find the biggest // right circular cylinder that can // be inscribed within a right circular // cone which in turn is inscribed // within a cube using System; class GFG { // Function to find the biggest // right circular cylinder static float cyl(float a) { // side cannot be negative if (a < 0) return -1; // radius of right circular cylinder float r = (2 * a * (float)(Math.Sqrt (2)) / 3); // height of right circular cylinder float h = (2 * a) / 3; // volume of right circular cylinder float V =(3.14f * (float)(Math.Pow(r, 2) * h)); return V; } // Driver code public static void Main() { float a = 5; Console.Write(cyl(a)); } } // This code is contributed by Rajput-Ji
PHP
<?php // PHP Program to find the biggest right // circular cylinder that can be inscribed // within a right circular cone which in // turn is inscribed within a cube // Function to find the biggest // right circular cylinder function cyl( $a ) { // side cannot be negative if ($a < 0) return -1; // radius of right circular cylinder $r = (2 * $a * sqrt(2)) / 3; // height of right circular cylinder $h = (2 * $a) / 3; // volume of right circular cylinder $V = 3.14 * pow($r, 2) * $h; return $V; } // Driver code $a = 5; echo cyl($a); // This code is contributed by Mahadev99 ?>
Javascript
<script> // javascript Program to find the biggest right circular // cylinder that can be inscribed within a right // circular cone which in turn is inscribed // within a cube // Function to find the biggest // right circular cylinder function cyl(a) { // side cannot be negative if (a < 0) return -1; // radius of right circular cylinder var r = (2 * a *(Math.sqrt (2)) / 3); // height of right circular cylinder var h = (2 * a) / 3; // volume of right circular cylinder var V =(3.14 *(Math.pow(r, 2) * h)); return V; } // Driver code var a = 5; document.write(cyl(a).toFixed(5)); // This code contributed by Princi Singh </script>
232.593
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