给定一个立方体的空间对角线长度d 。任务是计算给定空间对角线长度下立方体所占的体积。空间对角线是连接不在同一面上的两个顶点的线。
例子:
Input: d = 5
Output: Volume of Cube: 24.0563
Input: d = 10
Output: Volume of Cube: 192.45
Volume of cube whose space diagonal is given:
证明:
Let d = the length of diagonal |AB| and
let a = the length of each side of the cube.
Pythagorus #1 in triangle ACD:
Pythagorus #2 in triangle ABC:
Now we can solve for a in terms of d:
This means that the volume V is:
以下是所需的实现:
C++
// C++ program to find the volume occupied
// by Cube with given space diagonal
#include
using namespace std;
// Function to calculate Volume
float CubeVolume(float d)
{
float Volume;
// Formula to find Volume
Volume = (sqrt(3) * pow(d, 3)) / 9;
return Volume;
}
// Drivers code
int main()
{
// space diagonal of Cube
float d = 5;
cout << "Volume of Cube: "
<< CubeVolume(d);
return 0;
}
Java
// Java program to find the volume occupied
// by Cube with given space diagonal
public class GFG{
// Function to calculate Volume
static float CubeVolume(float d)
{
float Volume;
// Formula to find Volume
Volume = (float) (Math.sqrt(3) * Math.pow(d, 3)) / 9;
return Volume;
}
// Drivers code
public static void main(String []args)
{
// space diagonal of Cube
float d = 5;
System.out.println("Volume of Cube: " + CubeVolume(d));
}
// This code is contributed by Ryuga
}
Python3
# Python 3 program to find the volume occupied
# by Cube with given space diagonal
from math import sqrt, pow
# Function to calculate Volume
def CubeVolume(d):
# Formula to find Volume
Volume = (sqrt(3) * pow(d, 3)) / 9
return Volume
# Drivers code
if __name__ == '__main__':
# space diagonal of Cube
d = 5
print("Volume of Cube:",'{0:.6}' .
format(CubeVolume(d)))
# This code is contributed
# by SURENDRA_GANGWAR
C#
// C# program to find the volume occupied
// by Cube with given space diagonal
using System;
public class GFG{
// Function to calculate Volume
static float CubeVolume(float d)
{
float Volume;
// Formula to find Volume
Volume = (float) (Math.Sqrt(3) * Math.Pow(d, 3)) / 9;
return Volume;
}
// Drivers code
public static void Main()
{
// space diagonal of Cube
float d = 5;
Console.WriteLine("Volume of Cube: {0:F4}" , CubeVolume(d));
}
// This code is contributed by mits
}
PHP
Javascript
输出:
Volume of Cube: 24.0563
时间复杂度: O(1)