给定两个代表圆锥高度和半径的整数H1和R ,以及代表金字塔底部高度和长度的两个整数H2和S ,任务是找到圆锥和金字塔的斜高。
例子:
Input: H1 = 4.5, R = 6, H2 = 4, S = 4.8
Output:
Slant height of cone is: 7.5
Slant height of pyramid is: 4.66476
Input: H1 = 2, R = 4, H2 = 4, S = 8
Output:
Slant height of cone is: 4.47214
Slant height of pyramid is: 5.65685
方法:圆锥或棱锥等物体的倾斜高度是从沿侧面的任何顶点到底部(沿面的中心)测量的距离。直圆锥的斜高在整个表面上是均匀的,由以下公式给出:
where,
L is the slant height of the right circular cone
R is the radius of the right circular cone and
H is the height of the right circular cone
金字塔的斜高由以下公式给出:
where,
L is the slant height of the pyramid
S is the side of the base of the pyramid
H is the height of the pyramid
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to calculate slant
// height of a cone
void coneSlantHeight(double cone_h,
double cone_r)
{
// Store the slant height of cone
double slant_height_cone
= sqrt(pow(cone_h, 2)
+ pow(cone_r, 2));
// Print the result
cout << "Slant height of cone is: "
<< slant_height_cone << '\n';
}
// Function to find the slant
// height of a pyramid
void pyramidSlantHeight(double pyramid_h,
double pyramid_s)
{
// Store the slant height of pyramid
double slant_height_pyramid
= sqrt(pow(pyramid_s / 2, 2)
+ pow(pyramid_h, 2));
// Print the result
cout << "Slant height of pyramid is: "
<< slant_height_pyramid << '\n';
}
// Driver Code
int main()
{
// Dimensions of Cone
double H1 = 4.5, R = 6;
// Function Call for slant height
// of Cone
coneSlantHeight(H1, R);
// Dimensions of Pyramid
double H2 = 4, S = 4.8;
// Function to calculate
// slant height of a pyramid
pyramidSlantHeight(H2, S);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
class GFG
{
// Function to calculate slant
// height of a cone
static void coneSlantHeight(double cone_h,
double cone_r)
{
// Store the slant height of cone
double slant_height_cone
= Math.sqrt(Math.pow(cone_h, 2)
+ Math.pow(cone_r, 2));
// Print the result
System.out.println("Slant height of cone is: " +
slant_height_cone);
}
// Function to find the slant
// height of a pyramid
static void pyramidSlantHeight(double pyramid_h,
double pyramid_s)
{
// Store the slant height of pyramid
double slant_height_pyramid
= Math.sqrt(Math.pow(pyramid_s / 2, 2)
+ Math.pow(pyramid_h, 2));
// Print the result
System.out.println("Slant height of pyramid is: " +
slant_height_pyramid);
}
// Driver Code
public static void main (String[] args)
{
// Dimensions of Cone
double H1 = 4.5, R = 6;
// Function Call for slant height
// of Cone
coneSlantHeight(H1, R);
// Dimensions of Pyramid
double H2 = 4, S = 4.8;
// Function to calculate
// slant height of a pyramid
pyramidSlantHeight(H2, S);
}
}
// This code is contributed by AnkThon
Python3
# Python 3 program for the above approach
from math import sqrt,pow
# Function to calculate slant
# height of a cone
def coneSlantHeight(cone_h, cone_r):
# Store the slant height of cone
slant_height_cone = sqrt(pow(cone_h, 2) + pow(cone_r, 2))
# Print the result
print("Slant height of cone is:",slant_height_cone)
# Function to find the slant
# height of a pyramid
def pyramidSlantHeight(pyramid_h, pyramid_s):
# Store the slant height of pyramid
slant_height_pyramid = sqrt(pow(pyramid_s/2, 2) + pow(pyramid_h, 2))
# Print the result
print("Slant height of pyramid is:","{:.5f}".format(slant_height_pyramid))
# Driver Code
if __name__ == '__main__':
# Dimensions of Cone
H1 = 4.5
R = 6
# Function Call for slant height
# of Cone
coneSlantHeight(H1, R);
# Dimensions of Pyramid
H2 = 4
S = 4.8
# Function to calculate
# slant height of a pyramid
pyramidSlantHeight(H2, S)
C#
// C# program for the above approach
using System;
public class GFG
{
// Function to calculate slant
// height of a cone
static void coneSlantHeight(double cone_h,
double cone_r)
{
// Store the slant height of cone
double slant_height_cone
= Math.Sqrt(Math.Pow(cone_h, 2)
+ Math.Pow(cone_r, 2));
// Print the result
Console.WriteLine("Slant height of cone is: " +
slant_height_cone);
}
// Function to find the slant
// height of a pyramid
static void pyramidSlantHeight(double pyramid_h,
double pyramid_s)
{
// Store the slant height of pyramid
double slant_height_pyramid
= Math.Sqrt(Math.Pow(pyramid_s / 2, 2)
+ Math.Pow(pyramid_h, 2));
// Print the result
Console.WriteLine("Slant height of pyramid is: " +
slant_height_pyramid);
}
// Driver Code
public static void Main (string[] args)
{
// Dimensions of Cone
double H1 = 4.5, R = 6;
// Function Call for slant height
// of Cone
coneSlantHeight(H1, R);
// Dimensions of Pyramid
double H2 = 4, S = 4.8;
// Function to calculate
// slant height of a pyramid
pyramidSlantHeight(H2, S);
}
}
// This code is contributed by AnkThon
Javascript
Slant height of cone is: 7.5
Slant height of pyramid is: 4.66476
时间复杂度: O(1)
辅助空间: O(1)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。