📜  找到我们可以倾斜瓶子而不会洒水的最大角度

📅  最后修改于: 2021-04-29 13:58:46             🧑  作者: Mango

给定一个水瓶呈矩形棱柱形,其底边为边长为x cm且高度为y cm的正方形,任务是找到当z立方厘米为z立方厘米时,我们可以倾斜水瓶而不漏水的最大角度。将水倒入瓶中,并逐渐将瓶围绕底座的一侧倾斜。

例子:

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 spilt when we tilt
the bottle more than 45 degrees.

Input: x = 12, y = 21, z = 10
Output: 89.78346

方法:

首先,我们需要知道水的表面始终与地面平行。因此,如果瓶子倾斜,水将保持与地面平行。

这里有2种情况:-

  1. 当水量小于瓶子总体积的一半时,我们考虑瓶子内的水所形成的右下角三角形,并借助切线反函数计算所需的角度。
  2. 当水大于或等于瓶子的总体积时,我们考虑由瓶子内的空白空间形成的右上角三角形。现在我们可以轻松地计算角度了。

下面是上述方法的实现:

C++
// C++ program to find the maximum angle
// at which we can tilt the bottle
// without spilling any water
  
#include 
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 Base square side length
    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


输出:
89.7835