📜  形成空心矩形棱镜所需的最小块数

📅  最后修改于: 2021-05-24 17:42:10             🧑  作者: Mango

给定块L,BH的尺寸,任务是形成长度为A和高度为Ht的空心矩形棱柱,以使最少不需要块。
例子:

方法:
计算每一层所需的最小块数,这可以通过以下方式设置:将块设置为占据最大长度,方法是找到直角棱镜4个边所需的块数,然后选择块侧可以作为块的高度。从宽度或高度中取较大者作为高度。
下面是上述方法的实现:

C++
// C++ Implementation to find the minimum
// no of blocks required to form
// hollow rectangular prism
#include 
using namespace std;
 
// Function to display output
void disp(int row_no, int block)
{
    cout << row_no * block;
}
 
// Function to return minimum no of layers
// required to form the hollow prism
int row(int ht, int h)
{
    return ht / h;
}
 
// Function to calculate no of blocks
// required for each layer
void calculate(int l, int w, int h, int a, int ht)
{
    // No of blocks required for each row
    int no_block = (4 * a) / l;
    int row_no;
 
    // Check for no of layers is minimum
    if (h < w)
        row_no = row(ht, w);
    else
        row_no = row(ht, h);
    disp(row_no, no_block);
}
 
// Driver function
int main()
{
    // Length, width, height of each block
    int l = 50, w = 20, h = 35;
 
    // Side of one wall
    int a = 700;
 
    // height of each wall
    int ht = 140;
 
    calculate(l, w, h, a, ht);
    return 0;
}


Java
// Java Implementation to find the minimum
// no of blocks required to form
// hollow rectangular prism
import java.util.*;
 
class GFG{
  
// Function to display output
static void disp(int row_no, int block)
{
    System.out.print(row_no * block);
}
  
// Function to return minimum no of layers
// required to form the hollow prism
static int row(int ht, int h)
{
    return ht / h;
}
  
// Function to calculate no of blocks
// required for each layer
static void calculate(int l, int w, int h, int a, int ht)
{
    // No of blocks required for each row
    int no_block = (4 * a) / l;
    int row_no;
  
    // Check for no of layers is minimum
    if (h < w)
        row_no = row(ht, w);
    else
        row_no = row(ht, h);
    disp(row_no, no_block);
}
  
// Driver function
public static void main(String[] args)
{
    // Length, width, height of each block
    int l = 50, w = 20, h = 35;
  
    // Side of one wall
    int a = 700;
  
    // height of each wall
    int ht = 140;
  
    calculate(l, w, h, a, ht);
}
}
 
// This code is contributed by PrinciRaj1992


Python 3
# Python 3 Implementation to find the minimum
# no of blocks required to form
# hollow rectangular prism
 
# Function to display output
def disp(row_no,block):
    print(row_no * block)
 
# Function to return minimum no of layers
# required to form the hollow prism
def row(ht, h):
    return ht // h
 
# Function to calculate no of blocks
# required for each layer
def calculate(l, w, h, a, ht):
     
        # No of blocks required for each row
    no_block = (4 * a) // l
     
    # Check for no of layers is minimum
    if (h < w):
        row_no = row(ht, w)
    else:
        row_no = row(ht, h)
    disp(row_no, no_block)
 
# Driver function
if __name__ == '__main__':
    # Length, width, height of each block
    l = 50
    w = 20
    h = 35
 
    # Side of one wall
    a = 700
 
    # height of each wall
    ht = 140
 
    calculate(l, w, h, a, ht)
 
# This code is contributed by Surendra_Gangwar


C#
// C# Implementation to find the minimum
// no of blocks required to form
// hollow rectangular prism
using System;
 
class GFG{
 
// Function to display output
static void disp(int row_no, int block)
{
    Console.Write(row_no * block);
}
 
// Function to return minimum no of layers
// required to form the hollow prism
static int row(int ht, int h)
{
    return ht / h;
}
 
// Function to calculate no of blocks
// required for each layer
static void calculate(int l, int w, int h,
                        int a, int ht)
{
    // No of blocks required for each row
    int no_block = (4 * a) / l;
    int row_no;
 
    // Check for no of layers is minimum
    if (h < w)
        row_no = row(ht, w);
    else
        row_no = row(ht, h);
    disp(row_no, no_block);
}
 
// Driver function
public static void Main(String[] args)
{
    // Length, width, height of each block
    int l = 50, w = 20, h = 35;
 
    // Side of one wall
    int a = 700;
 
    // height of each wall
    int ht = 140;
 
    calculate(l, w, h, a, ht);
}
}
 
// This code is contributed by PrinciRaj1992


Javascript


输出:
224

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。