Número mínimo de bloques necesarios para formar un prisma rectangular hueco

Dadas las dimensiones de un bloque L, B y H , la tarea es formar un prisma rectangular hueco de longitud A y altura Ht tal que se requiera un número mínimo de bloques.
Ejemplos: 
 

Entrada: L = 40, B = 30, H = 10 y A = 500, Ht = 300 
Salida: 500
Entrada: L = 30, B = 20, H = 20 y A = 600, Ht = 240 
Salida: 960 
 

Enfoque: 
Calcule el número mínimo de bloques requeridos en cada capa, lo que se puede hacer colocando los bloques de tal manera que ocupe la longitud máxima al encontrar el número de bloques requeridos para los 4 lados del prisma rectangular, luego elija para el bloque lado que se puede tomar como la altura del bloque. Del ancho o del alto se toma como alto el que sea mayor. 
A continuación se muestra la implementación del enfoque anterior: 
 

C++

// C++ Implementation to find the minimum
// no of blocks required to form
// hollow rectangular prism
#include <bits/stdc++.h>
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

<script>
// javascript Implementation to find the minimum
// no of blocks required to form
// hollow rectangular prism
 
// Function to display output
function disp( row_no,  block)
{
     document.write( row_no * block);
}
 
// Function to return minimum no of layers
// required to form the hollow prism
function row( ht,  h)
{
    return ht / h;
}
 
// Function to calculate no of blocks
// required for each layer
function calculate( l,  w,  h,  a,  ht)
{
 
    // No of blocks required for each row
    let no_block = (4 * a) / l;
    let 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
 
    // Length, width, height of each block
    let l = 50, w = 20, h = 35;
 
    // Side of one wall
    let a = 700;
 
    // height of each wall
    let ht = 140;
    calculate(l, w, h, a, ht);
        
// This code is contributed by Rajput-Ji
 
</script>
Producción: 

224

 

Publicación traducida automáticamente

Artículo escrito por chsadik99 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *