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

📅  最后修改于: 2021-04-27 21:53:39             🧑  作者: Mango

给定三个数字NMa 。查找尺寸的平方数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


输出:
4