给定长方体的长度、宽度和高度,任务是找到可以放入长方体的最长杆的长度。
例子:
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
Javascript
输出:
17
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。