Dada una botella de agua con forma de prisma rectangular cuya base es un cuadrado de lado x cm y altura y cm, la tarea es encontrar el ángulo máximo al que podemos inclinar la botella sin derramar agua, cuando z cm cúbicos de Se vierte agua en la botella y se inclina gradualmente la botella alrededor de uno de los lados de la base.
Ejemplos:
Input: x = 2, y = 2, z = 4 Output: 45.00000 Explanation: This bottle has a cubic shape, and it is half-full. The water gets split when we tilt the bottle more than 45 degrees. Input: x = 12, y = 21, z = 10 Output: 89.78346
Enfoque:
En primer lugar, debemos saber que la superficie del agua siempre está paralela al suelo. De modo que si la botella se inclina el agua permanecerá paralela al suelo.
Aquí tenemos 2 casos: –
- Cuando el agua es menos de la mitad del volumen total de la botella, entonces consideramos el triángulo rectángulo inferior formado por el agua dentro de la botella y con la ayuda de la tangente inversa calculamos el ángulo requerido.
- Cuando el agua es mayor o igual al volumen total de la botella, entonces consideramos el triángulo rectángulo superior formado por el espacio vacío dentro de la botella. Ahora podemos calcular el ángulo fácilmente.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ program to find the maximum angle // at which we can tilt the bottle // without spilling any water #include <bits/stdc++.h> using namespace std; float find_angle(int x, int y, int z) { // Now we have the volume // of rectangular prism a*a*b int volume = x * x * y; float ans = 0; // Now we have 2 cases! if (z < volume / 2) { float d = (x * y * y) / (2.0 * z); // Taking the tangent inverse of value d // As we want to take out the required angle ans = atan(d); } else { z = volume - z; float d = (2 * z) / (float)(x * x * x); // Taking the tangent inverse of value d // As we want to take out the required angle ans = atan(d); } // As of now the angle is in radian. // So we have to convert it in degree. ans = (ans * 180) / 3.14159265; return ans; } int main() { // Enter the Base square side length int x = 12; // Enter the Height of rectangular prism int y = 21; // Enter the volume of water in the bottle int z = 10; cout << find_angle(x, y, z) << endl; }
Java
// Java program to find the maximum angle // at which we can tilt the bottle // without spilling any water class GFG { static float find_angle(int x, int y, int z) { // Now we have the volume // of rectangular prism a*a*b int volume = x * x * y; float ans = 0; // Now we have 2 cases! if (z < volume / 2) { float d = (float) ((x * y * y) / (2.0 * z)); // Taking the tangent inverse of value d // As we want to take out the required angle ans = (float) Math.atan(d); } else { z = volume - z; float d = (2 * z) / (float) (x * x * x); // Taking the tangent inverse of value d // As we want to take out the required angle ans = (float) Math.atan(d); } // As of now the angle is in radian. // So we have to convert it in degree. ans = (float) ((ans * 180) / 3.14159265); return ans; } // Driver Code public static void main(String[] args) { // Enter the Base square side length int x = 12; // Enter the Height of rectangular prism int y = 21; // Enter the Base square side length int z = 10; System.out.print(find_angle(x, y, z) + "\n"); } } // This code is contributed by PrinciRaj1992
Python3
# Python3 program to find the maximum angle # at which we can tilt the bottle # without spilling any water from math import * def find_angle(x, y, z) : # Now we have the volume # of rectangular prism a*a*b volume = x * x * y; ans = 0; # Now we have 2 cases! if (z < volume // 2) : d = (x * y * y) / (2.0 * z); # Taking the tangent inverse of value d # As we want to take out the required angle ans = atan(d); else : z = volume - z; d = (2 * z) / (float)(x * x * x); # Taking the tangent inverse of value d # As we want to take out the required angle ans = atan(d); # As of now the angle is in radian. # So we have to convert it in degree. ans = (ans * 180) / 3.14159265; return round(ans, 4); # Driver Code if __name__ == "__main__" : # Enter the Base square side length x = 12; # Enter the Height of rectangular prism y = 21; # Enter the Base square side length z = 10; print(find_angle(x, y, z)); # This code is contributed by AnkitRai01
C#
// C# program to find the maximum angle // at which we can tilt the bottle // without spilling any water using System; class GFG { static float find_angle(int x, int y, int z) { // Now we have the volume // of rectangular prism a*a*b int volume = x * x * y; float ans = 0; // Now we have 2 cases! if (z < volume / 2) { float d = (float) ((x * y * y) / (2.0 * z)); // Taking the tangent inverse of value d // As we want to take out the required angle ans = (float) Math.Atan(d); } else { z = volume - z; float d = (2 * z) / (float) (x * x * x); // Taking the tangent inverse of value d // As we want to take out the required angle ans = (float) Math.Atan(d); } // As of now the angle is in radian. // So we have to convert it in degree. ans = (float) ((ans * 180) / 3.14159265); return ans; } // Driver Code public static void Main(String[] args) { // Enter the Base square side length int x = 12; // Enter the Height of rectangular prism int y = 21; // Enter the Base square side length int z = 10; Console.Write(find_angle(x, y, z) + "\n"); } } // This code is contributed by Rajput-Ji
Javascript
<script> // javascript program to find the maximum angle // at which we can tilt the bottle // without spilling any water function find_angle(x , y , z) { // Now we have the volume // of rectangular prism a*a*b var volume = x * x * y; var ans = 0; // Now we have 2 cases! if (z < volume / 2) { var d = ((x * y * y) / (2.0 * z)); // Taking the tangent inverse of value d // As we want to take out the required angle ans = Math.atan(d); } else { z = volume - z; var d = (2 * z) / (x * x * x); // Taking the tangent inverse of value d // As we want to take out the required angle ans = Math.atan(d); } // As of now the angle is in radian. // So we have to convert it in degree. ans = ((ans * 180) / 3.14159265); return ans; } // Driver Code // Enter the Base square side length var x = 12; // Enter the Height of rectangular prism var y = 21; // Enter the Base square side length var z = 10; document.write(find_angle(x, y, z).toFixed(4) + "\n"); // This code is contributed by todaysgaurav </script>
89.7835
Complejidad de tiempo: O(1)
Espacio Auxiliar: O(1)
Publicación traducida automáticamente
Artículo escrito por mayank17bcs1149 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA