📌  相关文章
📜  填充矩形地板所需的最少方形瓷砖数量

📅  最后修改于: 2021-10-23 08:36:14             🧑  作者: Mango

给定 (MXN) 米的矩形地板将铺设 (s X s) 的方形瓷砖。任务是找到铺设矩形地板所需的最少瓷砖数量。

约束:

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

例子:

方法:
给定每个瓦片的边必须与瓦片的边平行允许我们分别分析 X 轴和 Y 轴,即需要多少长度为 ‘s’ 的段来覆盖一段长度’和’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


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


Javascript


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


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


Javascript


输出:
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

蟒蛇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


Javascript


输出:
12