📜  填充矩形地板所需的最小正方形瓷砖数

📅  最后修改于: 2021-05-06 07:57:12             🧑  作者: Mango

给定(MXN)米的矩形地板,应使用(s X s)的方砖铺砌。任务是找到铺装矩形地板所需的最少瓷砖数。

限制条件:

  1. 允许覆盖比地板大的表面,但必须覆盖地板。
  2. 不允许破坏瓷砖。
  3. 瓷砖的侧面应与地板的侧面平行。

例子:

方法:
假定每个图块的边缘必须平行于图块的边缘,这使我们能够分别分析X轴和Y轴,即,需要多少个长度为“ s”的段才能覆盖一个长度为“ n”和“ N”的段—取这两个数量的乘积。

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

,其中ceil(x)是大于或等于x的最小整数。仅使用整数,通常写为

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

下面是上述方法的实现:

C++
// C++ implementation of above approach
#include 
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


Python 3
# 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


C++
// C++ implementation of above approach
#include 
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


Python 3
# 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


输出:
12


使用ceil函数:

C++

// C++ implementation of above approach
#include 
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

的Python 3

# 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


输出:
12