📜  可以放入长方体的最长杆的长度

📅  最后修改于: 2021-04-24 03:42:40             🧑  作者: Mango

给定长方体的长度,宽度和高度,任务是找到可以放入长方体的最长杆的长度。

例子:

Input: length = 12, breadth = 9, height = 8
Output: 17

Input: length = 22, breadth = 19, height = 8
Output: 30.1496

说明:在图中

GH = length, GF = breadth, FB= height

最长杆的长度为BH。因此,要计算BH,我们可以在三角BHF中应用毕达哥拉斯定理。从三角形HGF,我们可以计算FH的长度。

得到FH的长度后,我们可以使用三角BHF中的毕达哥拉斯定理找到BH的长度。

下面是上述方法的实现:

C++
// C++ program to find the longest rod
// that can fit in a cuboid
#include 
using namespace std;
  
// Function to find the length
double longestRodInCuboid(int length, 
                        int breadth, int height)
{
    double result;
    int temp;
  
    // temporary variable to hold
    // the intermediate result
    temp = length * length + breadth * breadth 
           + height * height;
  
    // length of longest rod is calculated
    // using square root function
    result = sqrt(temp);
  
    return result;
}
  
// Driver code
int main()
{
    int length = 12, breadth = 9, height = 8;
  
    // calling longestRodInCuboid() function to
    // get the length of longest rod
    cout << longestRodInCuboid(length, breadth, height);
  
    return 0;
}


Java
// Java program to find the longest
// rod that can fit in a cuboid
class GFG 
{
  
// Function to find the length
static double longestRodInCuboid(int length, 
                                 int breadth, 
                                 int height)
{
    double result;
    int temp;
      
    // temporary variable to hold
    // the intermediate result
    temp = length * length + breadth * 
           breadth + height * height;
      
    // length of longest rod is calculated
    // using square root function
    result = Math.sqrt(temp);
      
    return result;
}
  
// Driver Code
public static void main(String[] args) 
{
    int length = 12, 
        breadth = 9,
        height = 8;
  
    // calling longestRodInCuboid() 
    // function to get the length 
    // of longest rod
    System.out.println((int)longestRodInCuboid(length,
                                    breadth, height));
}
}
  
// This code is contributed by ChitraNayal


Python 3
# Python 3 program to find the longest rod 
# that can fit in a cuboid
  
# from math lib. import everything
from math import *
  
# Function to find the length 
def longestRodInCuboid(length, breadth, height) :
  
    # temporary variable to hold 
    # the intermediate result
    temp = length * length + breadth  * breadth + height * height
  
    #  length of longest rod is calculated 
    # using square root function
    result = sqrt(temp)
  
    return result
  
  
# Driver Code
if __name__ == "__main__" :
  
    length, breadth, height = 12, 9, 8
  
    # calling longestRodInCuboid() function to 
    # get the length of longest rod
    print(longestRodInCuboid(length, breadth, height))
  
  
# This code is contributed by ANKITRAI1


C#
// C# program to find the longest
// rod that can fit in a cuboid
using System;
class GFG 
{
  
// Function to find the length
static double longestRodInCuboid(int length, 
                                int breadth, 
                                int height)
{
    double result;
    int temp;
      
    // temporary variable to hold
    // the intermediate result
    temp = length * length + breadth * 
        breadth + height * height;
      
    // length of longest rod is calculated
    // using square root function
    result = Math.Sqrt(temp);
      
    return result;
}
  
// Driver Code
public static void Main() 
{
    int length = 12, 
        breadth = 9,
        height = 8;
  
    // calling longestRodInCuboid() 
    // function to get the length 
    // of longest rod
    Console.WriteLine((int)longestRodInCuboid(length,
                                    breadth, height));
}
}
  
// This code is contributed by inder_verma..


PHP


输出:
17