给定L , B和H表示长方体的长度,宽度和高度,任务是找到可以放置在一起以形成一个立方体的指定尺寸的最小长方体。
例子:
Input: L = 1, B = 1, H = 2
Output: 4
Explanation:
Volume of a cuboid of given dimensions = 1 * 1 * 2 = 2.
Volume of the cube that can be formed by combining these cuboids = 2 * 2 * 2 = 8.
Therefore, the number of cuboids required = 8 / 2 = 4.
Input: L = 2, B = 5, H = 10
Output: 10
原始的方法:找出最大的给定尺寸,并开始循环访问整数所获得的最大初始值。对于每个整数,检查它是否可以是可以由给定的长方体形成的立方体的可能尺寸。为此,请计算立方体的体积和由给定尺寸形成的长方体的体积。检查前者是否可被后者整除。如果发现为真,则将商打印为所需答案。
时间复杂度: O(L * B * H)
辅助空间: O(1)
高效方法:为了优化上述方法,该想法基于以下观察:
- 通过GIEN尺寸的长方体组合得到的立方体的最小长度等于L,B和H的LCM。这是因为立方体的尺寸必须可以被L , B和H整除。
- 为了找到所需的长方体数量,计算立方体的体积(= LCM(L,B,H) 3 )和长方体(= L * B * H)并打印(立方体的体积)/(的体积长方体)一个必需的答案。
下面是上述方法的实现:
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
输出:
4
时间复杂度: O(log(min(L,B,H)))
辅助空间: O(1)