Número mínimo de baldosas cuadradas requeridas para llenar el piso rectangular

Dado un piso rectangular de (MXN) metros se va a pavimentar con losetas cuadradas de (s X s). La tarea es encontrar el número mínimo de baldosas necesarias para pavimentar el suelo rectangular. 

Restricciones: 

  1. Se permite cubrir la superficie más grande que el piso, pero el piso tiene que estar cubierto.
  2. No está permitido romper las baldosas.
  3. Los lados de las baldosas deben estar paralelos a los lados del piso.

Ejemplos:  

Entrada: 2 1 2 
Salida:
largo del piso = 2 
ancho del piso = 1 
largo del lado de la baldosa = 2  El
número de baldosas necesarias para pavimentar es 2.

Entrada: 222 332 5 
Salida: 3015 
 

Planteamiento: 
Se da que los bordes de cada mosaico deben ser paralelos a los bordes de los mosaicos nos permite analizar los ejes X e Y por separado, es decir, cuantos segmentos de longitud ‘s’ se necesitan para cubrir un segmento de longitud’ y ‘ N’ — y tome el producto de estas dos cantidades.  

ceil(M/s) * ceil(N/s)

, donde ceil(x) es el menor entero superior o igual a x. Usando solo números enteros, generalmente se escribe como 

((M + s - 1) / s)*((N + s - 1) / s)

A continuación se muestra la implementación del enfoque anterior: 

C++

// C++ implementation of above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the number of tiles
int solve(int M, int N, int s)
{
    // if breadth is divisible by side of square
    if (N % s == 0) {
 
        // tiles required is N/s
        N = N / s;
    }
    else {
 
        // one more tile required
        N = (N / s) + 1;
    }
 
    // if length is divisible by side of square
    if (M % s == 0) {
 
        // tiles required is M/s
        M = M / s;
    }
    else {
        // one more tile required
        M = (M / s) + 1;
    }
 
    return M * N;
}
 
// Driver Code
int main()
{
    // input length and breadth of
    // rectangle and side of square
    int N = 12, M = 13, s = 4;
 
    cout << solve(M, N, s);
 
    return 0;
}

Java

// Java implementation
// of above approach
import java.util.*;
import java.lang.*;
import java.io.*;
 
class GFG
{
     
// Function to find the
// number of tiles
static int solve(int M, int N, int s)
{
    // if breadth is divisible
    // by side of square
    if (N % s == 0)
    {
 
        // tiles required is N/s
        N = N / s;
    }
    else
    {
 
        // one more tile required
        N = (N / s) + 1;
    }
 
    // if length is divisible
    // by side of square
    if (M % s == 0)
    {
 
        // tiles required is M/s
        M = M / s;
    }
    else
    {
         
        // one more tile required
        M = (M / s) + 1;
    }
 
    return M * N;
}
 
// Driver Code
public static void main(String args[])
{
    // input length and breadth of
    // rectangle and side of square
    int N = 12, M = 13, s = 4;
 
    System.out.println(solve(M, N, s));
}
}
 
// This code is contributed
// by ChitraNayal

Python3

# Python 3 implementation of
# above approach
 
# Function to find the number
# of tiles
def solve(M, N, s) :
     
    # if breadth is divisible
    # by side of square
    if (N % s == 0) :
         
        # tiles required is N/s
        N = N // s
         
    else :
         
        # one more tile required
        N = (N // s) + 1
 
    # if length is divisible by
    # side of square
    if (M % s == 0) :
         
        # tiles required is M/s
        M = M // s
         
    else :
         
        # one more tile required
        M = (M // s) + 1
     
    return M * N
 
# Driver Code
if __name__ == "__main__" :
     
    # input length and breadth of
    # rectangle and side of square
    N, M, s = 12, 13, 4
 
    print(solve(M, N, s))
             
# This code is contributed by ANKITRAI1

C#

// C# implementation of above approach
using System;
 
class GFG
{
     
// Function to find the
// number of tiles
static int solve(int M, int N, int s)
{
    // if breadth is divisible
    // by side of square
    if (N % s == 0)
    {
 
        // tiles required is N/s
        N = N / s;
    }
    else
    {
 
        // one more tile required
        N = (N / s) + 1;
    }
 
    // if length is divisible
    // by side of square
    if (M % s == 0)
    {
 
        // tiles required is M/s
        M = M / s;
    }
    else
    {
         
        // one more tile required
        M = (M / s) + 1;
    }
 
    return M * N;
}
 
// Driver Code
static void Main()
{
    // input length and breadth of
    // rectangle and side of square
    int N = 12, M = 13, s = 4;
 
    Console.WriteLine(solve(M, N, s));
}
}
 
// This code is contributed
// by mits

PHP

<?php
// PHP implementation of above approach
 
// Function to find the number of tiles
function solve($M, $N, $s)
{
    // if breadth is divisible
    // by side of square
    if ($N % $s == 0)
    {
 
        // tiles required is N/s
        $N = $N / $s;
    }
    else
    {
 
        // one more tile required
        $N = ($N / $s) + 1;
    }
 
    // if length is divisible
    // by side of square
    if ($M % $s == 0)
    {
 
        // tiles required is M/s
        $M = $M / $s;
    }
    else
    {
         
        // one more tile required
        $M = ($M / $s) + 1;
    }
 
    return (int)$M * $N;
}
 
// Driver Code
 
// input length and breadth of
// rectangle and side of square
$N = 12;
$M = 13;
$s = 4;
 
echo solve($M, $N, $s);
 
// This code is contributed by mits
?>

Javascript

<script>
 
// Javascript implementation
// of above approach
 
// Function to find the
// number of tiles
function solve(M, N, s)
{
     
    // If breadth is divisible
    // by side of square
    if (N % s == 0)
    {
         
        // Tiles required is N/s
        N = N / s;
    }
    else
    {
         
        // One more tile required
        N = (N / s) + 1;
    }
 
    // If length is divisible
    // by side of square
    if (M % s == 0)
    {
         
        // Tiles required is M/s
        M = M / s;
    }
    else
    {
         
        // One more tile required
        M = (M / s) + 1;
    }
    return parseInt(M * N);
}
 
// Driver Code
 
// Input length and breadth of
// rectangle and side of square
var N = 12, M = 13, s = 4;
 
document.write(solve(M, N, s));
 
// This code is contributed by todaysgaurav
 
</script>
Producción: 

12

 

Usando la función ceil : 

C++

// C++ implementation of above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the number of tiles
int solve(double M, double N, double s)
{
    // no of tiles
    int ans = ((int)(ceil(M / s)) * (int)(ceil(N / s)));
 
    return ans;
}
 
// Driver Code
int main()
{
    // input length and breadth of
    // rectangle and side of square
    double N = 12, M = 13, s = 4;
 
    cout << solve(M, N, s);
 
    return 0;
}

Java

// Java implementation of above approach
class GFG
{
// Function to find the number of tiles
static int solve(double M,
                 double N, double s)
{
    // no of tiles
    int ans = ((int)(Math.ceil(M / s)) *
               (int)(Math.ceil(N / s)));
 
    return ans;
}
 
// Driver Code
public static void main(String[] args)
{
    // input length and breadth of
    // rectangle and side of square
    double N = 12, M = 13, s = 4;
 
    System.out.println(solve(M, N, s));
}
}
 
// This Code is contributed by mits

Python3

# Python 3 implementation of
# above approach
import math
 
# Function to find the
# number of tiles
def solve(M, N, s):
 
    # no of tiles
    ans = ((math.ceil(M / s)) *
           (math.ceil(N / s)));
 
    return ans
 
# Driver Code
if __name__ == "__main__":
     
    # input length and breadth of
    # rectangle and side of square
    N = 12
    M = 13
    s = 4
 
    print(solve(M, N, s))
 
# This code is contributed
# by ChitraNayal

C#

// C# implementation of above approach
using System;
class GFG
{
// Function to find the number of tiles
static int solve(double M,
                double N, double s)
{
    // no of tiles
    int ans = ((int)(Math.Ceiling(M / s)) *
            (int)(Math.Ceiling(N / s)));
 
    return ans;
}
 
// Driver Code
public static void Main()
{
    // input length and breadth of
    // rectangle and side of square
    double N = 12, M = 13, s = 4;
 
    Console.WriteLine(solve(M, N, s));
}
}
 
// This Code is contributed by mits

PHP

<?php
// PHP implementation of
// above approach
 
// Function to find the
// number of tiles
function solve($M, $N, $s)
{
    // no of tiles
    $ans = ((int)(ceil($M / $s)) *
            (int)(ceil($N / $s)));
 
    return $ans;
}
 
// Driver Code
 
// input length and breadth of
// rectangle and side of square
$N = 12;
$M = 13;
$s = 4;
 
echo solve($M, $N, $s);
 
// This code is contributed by mits
?>

Javascript

<script>
 
// Javascript implementation of above approach
     
// Function to find the number of tiles
function solve(M, N, s)
{
     
    // No of tiles
    let ans = Math.floor(((Math.ceil(M / s)) *
                          (Math.ceil(N / s))));
     
    return ans;
}
 
// Driver Code
 
// Input length and breadth of
// rectangle and side of square
let N = 12, M = 13, s = 4;
 
document.write(solve(M, N, s));
 
// This code is contributed by rag2127
 
</script>
Producción: 

12

 

Publicación traducida automáticamente

Artículo escrito por Subash23 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 *