📜  覆盖 N*M 矩形所需的边长正方形数

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

给定三个数字N  , M  , a  .求维度的平方数a*a  需要覆盖N * M  矩形。
注意

  • 允许覆盖大于矩形的表面,但必须覆盖矩形。
  • 不允许打破一个正方形。
  • 正方形的边应该与长方形的边平行。

例子

Input: N = 6, M = 6, a = 4
Output: 4

Input: N = 2, M = 3, a = 1
Output: 6

方法:一种有效的方法是进行观察并找到一个公式。每个正方形的边必须与矩形的边平行的约束允许分别分析 X 轴和 Y 轴,即需要多少个长度为 ‘a’ 的正方形来覆盖长度为 ‘m’ 和 ‘n’ 的正方形并取这两个量的乘积。覆盖“m”大小的正方形所需的边长为“a”的小正方形的数量是ceil(m/a)。类似地,覆盖“n”个大小的正方形所需的“a”个大小的正方形的数量是ceil(n/a)。
因此,答案将是 ceil(m/a)*ceil(n/a)。
下面是上述方法的实现:

C++
// CPP program to find number of squares
// of a*a required to cover n*m rectangle
#include 
using namespace std;
 
// function to find number of squares
// of a*a required to cover n*m rectangle
int Squares(int n, int m, int a)
{
    return ((m + a - 1) / a) * ((n + a - 1) / a);
}
 
// Driver code
int main()
{
    int n = 6, m = 6, a = 4;
 
    // function call
    cout << Squares(n, m, a);
 
    return 0;
}


Java
// Java program to find number of squares
// of a*a required to cover n*m rectangle
import java.util.*;
 
class solution
{
 
// function to find a number of squares
// of a*a required to cover n*m rectangle
static int Squares(int n, int m, int a)
{
 
    return ((m + a - 1) / a) * ((n + a - 1) / a);
 
}
 
// Driver code
public static void main(String arr[])
{
    int n = 6, m = 6, a = 4;
 
    // function call
    System.out.println(Squares(n, m, a));
 
}
 
}
//This code is contributed by Surendra_Gangwar


Python 3
# Python 3 program to find number
# of squares of a*a required to
# cover n*m rectangle
 
# function to find number of
# squares of a*a required to
# cover n*m rectangle
def Squares(n, m, a):
    return (((m + a - 1) // a) *
            ((n + a - 1) // a))
 
# Driver code
if __name__ == "__main__":
    n = 6
    m = 6
    a = 4
 
    # function call
    print(Squares(n, m, a))
 
# This code is contributed
# by ChitraNayal


C#
// CSHARP program to find number of squares
// of a*a required to cover n*m rectangle
 
using System;
 
class GFG
{
    // function to find a number of squares
    // of a*a required to cover n*m rectangle
    static int Squares(int n, int m, int a)
    {
     
        return ((m + a - 1) / a) * ((n + a - 1) / a);
     
    }
 
    static void Main()
    {
          int n = 6, m = 6, a = 4;
 
         // function call
         Console.WriteLine(Squares(n, m, a));
    }
    // This code is contributed by ANKITRAI1
}


PHP


Javascript


输出:
4