📌  相关文章
📜  将长方体分成立方体,使得体积总和最大

📅  最后修改于: 2021-10-26 06:07:18             🧑  作者: Mango

给定长方体的。任务是将给定的长方体分成最少数量的立方体,使所有立方体的大小相同并且立方体的体积总和最大。
例子:

Input : l = 2, b = 4, h = 6
Output : 2 6
A cuboid of length 2, breadth 4 and 
height 6 can be divided into 6 cube 
of side equal to 2.
Volume of cubes = 6*(2*2*2) = 6*8 = 48.
Volume of cuboid = 2*4*6 = 48.

Input : 1 2 3
Output : 1 6

首先,我们不允许浪费长方体的体积,因为我们需要最大体积总和。所以,每一边都应该被所有的立方体完全划分。由于立方体的三个边中的每一个都相等,所以立方体的每一边都需要被相同的数整除,比如 x,这将是立方体的边。因此,我们必须最大化这个 x,它将划分给定的长度、宽度和高度。只有当它是给定的长度、宽度和高度的最大公约数时,这个 x 才是最大的。因此,立方体的长度将是长、宽和高的 GCD。
现在,为了计算立方体的数量,我们知道长方体的总体积并且可以找到一个立方体的体积(因为边已经计算了)。因此,立方体的总数等于(立方体的体积)/(立方体的体积)即(l * b * h)/(x * x * x)。
下面是这种方法的实现:

C++
// CPP program to find optimal way to break
// cuboid into cubes.
#include 
using namespace std;
 
// Print the maximum side and no of cube.
void maximizecube(int l, int b, int h)
{
    // GCD to find side.
    int side = __gcd(l, __gcd(b, h));
 
    // dividing to find number of cubes.
    int num = l / side;
    num = (num * b / side);
    num = (num * h / side);
 
    cout << side << " " << num << endl;
}
 
// Driver code
int main()
{
    int l = 2, b = 4, h = 6;
 
    maximizecube(l, b, h);
    return 0;
}


Java
// JAVA Code for Divide cuboid into cubes
// such that sum of volumes is maximum
import java.util.*;
 
class GFG {
     
    static int gcd(int m, int n)
    {
        if(n == 0)
            return m;
        else if(n > m)
            return gcd(n,m);
        else
            return gcd(n, m % n);
    }
     
    // Print the maximum side and no
    //     of cube.
    static void maximizecube(int l, int b,
                                    int h)
    {
        // GCD to find side.
        int side = gcd(l, gcd(b, h));
      
        // dividing to find number of cubes.
        int num = l / side;
        num = (num * b / side);
        num = (num * h / side);
      
       System.out.println( side + " " + num);
    }
     
    /* Driver program  */
    public static void main(String[] args)
    {
         int l = 2, b = 4, h = 6;
         maximizecube(l, b, h);
    }
}
 
// This code is contributed by Arnav Kr. Mandal.


Python3
# Python3 code to find optimal way to break
# cuboid into cubes.
from fractions import gcd
 
# Print the maximum side and no of cube.
def maximizecube( l , b , h ):
 
    # GCD to find side.
    side = gcd(l, gcd(b, h))
     
    # dividing to find number of cubes.
    num = int(l / side)
    num = int(num * b / side)
    num = int(num * h / side)
     
    print(side, num)
 
# Driver code
l = 2
b = 4
h = 6
 
maximizecube(l, b, h)
 
# This code is contributed by "Sharad_Bhardwaj".


C#
// C# Code for Divide cuboid into cubes
// such that sum of volumes is maximum
using System;
 
class GFG {
     
    static int gcd(int m, int n)
    {
        if(n == 0)
            return m;
        else if(n > m)
            return gcd(n,m);
        else
            return gcd(n, m % n);
    }
     
    // Print the maximum side and no
    // of cube.
    static void maximizecube(int l, int b,
                                    int h)
    {
        // GCD to find side.
        int side = gcd(l, gcd(b, h));
     
        // dividing to find number of cubes.
        int num = l / side;
        num = (num * b / side);
        num = (num * h / side);
     
    Console.WriteLine( side + " " + num);
    }
     
    /* Driver program */
    public static void Main()
    {
        int l = 2, b = 4, h = 6;
        maximizecube(l, b, h);
    }
}
 
// This code is contributed by vt_m.


PHP
 $b)
        return __gcd($a - $b , $b ) ;
 
    return __gcd($a , $b - $a) ;
}
 
 
// Print the maximum side and no of cube.
function maximizecube($l, $b, $h)
{
     
    // GCD to find side.
    $side = __gcd($l, __gcd($b, $h));
 
    // dividing to find number of cubes.
    $num = $l / $side;
    $num = ($num * $b / $side);
    $num = ($num * $h / $side);
 
    echo $side , " " , $num ;
}
 
    // Driver code
    $l = 2;
    $b = 4;
    $h = 6;
    maximizecube($l, $b, $h);
     
// This code is contributed by anuj_67.
?>


Javascript


输出:

2 6

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程