📜  形成立方体所需的最少立方体数量

📅  最后修改于: 2021-10-23 08:11:36             🧑  作者: Mango

给定LBH表示长方体的,任务是找到可以放置在一起形成立方体的指定尺寸的长方体的最小数量。

例子:

朴素的方法:找到给定维度的最大值,并从获得的最大值开始迭代整数值。对于每个整数,检查它是否可以是由给定长方体形成的立方体的可能维度。为此,计算立方体的体积和由给定尺寸形成的长方体的体积。检查前者是否可以被后者整除。如果发现为真,则将商打印为所需答案。

时间复杂度: O(L * B * H)
辅助空间: O(1)

高效的方法:为了优化上述方法,该想法基于以下观察:

  • 通过GIEN尺寸的长方体组合得到的立方体最小长度等于L,B和HLCM。这是因为立方体的维度必须能被LBH整除。
  • 为了找到所需的长方体数量,计算立方体的体积(= LCM(L, B, H) 3长方体(= L * B * H)并打印(立方体的体积)/(立方体的体积) cuboid ) a 要求的答案。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to calculate and
// return LCM of a, b, and c
int find_lcm(int a, int b, int c)
{
    // Find GCD of a and b
    int g = __gcd(a, b);
 
    // Find LCM of a and b
    int LCM1 = (a * b) / g;
 
    // LCM(a, b, c) = LCM(LCM(a, b), c)
    g = __gcd(LCM1, c);
 
    // Finding LCM of a, b, c
    int LCM = (LCM1 * c) / g;
 
    // return LCM(a, b, c)
    return LCM;
}
 
// Function to find the minimum
// number of cuboids required to
// make the volume of a valid cube
void minimumCuboids(int L, int B, int H)
{
    // Find the LCM of L, B, H
    int lcm = find_lcm(L, B, H);
 
    // Volume of the cube
    int volume_cube = lcm * lcm * lcm;
 
    // Volume of the cuboid
    int volume_cuboid = L * B * H;
 
    // Minimum number cuboids required
    // to form a cube
    cout << (volume_cube / volume_cuboid);
}
 
// Driver Code
int main()
{
    // Given dimensions of cuboid
    int L = 1, B = 1, H = 2;
 
    // Function Call
    minimumCuboids(L, B, H);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
class GFG
{
 
// Function to calculate and
// return LCM of a, b, and c
static int find_lcm(int a, int b, int c)
{
   
    // Find GCD of a and b
    int g = __gcd(a, b);
 
    // Find LCM of a and b
    int LCM1 = (a * b) / g;
 
    // LCM(a, b, c) = LCM(LCM(a, b), c)
    g = __gcd(LCM1, c);
 
    // Finding LCM of a, b, c
    int LCM = (LCM1 * c) / g;
 
    // return LCM(a, b, c)
    return LCM;
}
 
// Function to find the minimum
// number of cuboids required to
// make the volume of a valid cube
static void minimumCuboids(int L, int B, int H)
{
   
    // Find the LCM of L, B, H
    int lcm = find_lcm(L, B, H);
 
    // Volume of the cube
    int volume_cube = lcm * lcm * lcm;
 
    // Volume of the cuboid
    int volume_cuboid = L * B * H;
 
    // Minimum number cuboids required
    // to form a cube
    System.out.print((volume_cube / volume_cuboid));
}
static int __gcd(int a, int b) 
{ 
    return b == 0 ? a:__gcd(b, a % b);    
}
   
// Driver Code
public static void main(String[] args)
{
   
    // Given dimensions of cuboid
    int L = 1, B = 1, H = 2;
 
    // Function Call
    minimumCuboids(L, B, H);
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python program for the above approach
 
# Function to calculate and
# return LCM of a, b, and c
def find_lcm(a, b, c):
 
    # Find GCD of a and b
    g = __gcd(a, b);
 
    # Find LCM of a and b
    LCM1 = (a * b) // g;
 
    # LCM(a, b, c) = LCM(LCM(a, b), c)
    g = __gcd(LCM1, c);
 
    # Finding LCM of a, b, c
    LCM = (LCM1 * c) // g;
 
    # return LCM(a, b, c)
    return LCM;
 
# Function to find the minimum
# number of cuboids required to
# make the volume of a valid cube
def minimumCuboids(L, B, H):
 
    # Find the LCM of L, B, H
    lcm = find_lcm(L, B, H);
 
    # Volume of the cube
    volume_cube = lcm * lcm * lcm;
 
    # Volume of the cuboid
    volume_cuboid = L * B * H;
 
    # Minimum number cuboids required
    # to form a cube
    print((volume_cube // volume_cuboid));
def __gcd(a, b):
    if(b == 0):
        return a;
    else:
        return __gcd(b, a % b);
 
# Driver Code
if __name__ == '__main__':
 
    # Given dimensions of cuboid
    L = 1; B = 1; H = 2;
 
    # Function Call
    minimumCuboids(L, B, H);
 
# This code contributed by shikhasingrajput


C#
// C# program for the above approach
using System;
class GFG
{
 
// Function to calculate and
// return LCM of a, b, and c
static int find_lcm(int a, int b, int c)
{
   
    // Find GCD of a and b
    int g = __gcd(a, b);
 
    // Find LCM of a and b
    int LCM1 = (a * b) / g;
 
    // LCM(a, b, c) = LCM(LCM(a, b), c)
    g = __gcd(LCM1, c);
 
    // Finding LCM of a, b, c
    int LCM = (LCM1 * c) / g;
 
    // return LCM(a, b, c)
    return LCM;
}
 
// Function to find the minimum
// number of cuboids required to
// make the volume of a valid cube
static void minimumCuboids(int L, int B, int H)
{
   
    // Find the LCM of L, B, H
    int lcm = find_lcm(L, B, H);
 
    // Volume of the cube
    int volume_cube = lcm * lcm * lcm;
 
    // Volume of the cuboid
    int volume_cuboid = L * B * H;
 
    // Minimum number cuboids required
    // to form a cube
    Console.Write((volume_cube / volume_cuboid));
}
static int __gcd(int a, int b) 
{ 
    return b == 0 ? a:__gcd(b, a % b);    
}
   
// Driver Code
public static void Main(String[] args)
{
   
    // Given dimensions of cuboid
    int L = 1, B = 1, H = 2;
 
    // Function Call
    minimumCuboids(L, B, H);
}
}
 
// This code is contributed by 29AjayKumar


Javascript


输出:
4

时间复杂度: O(log(min(L, B, H)))
辅助空间: O(1)