给定一个长为l 、宽为b的矩形,假设每个正方形的边长为a ,我们需要找到可以覆盖该矩形表面的最小正方形数。允许覆盖大于矩形的表面,但矩形必须被覆盖。不允许打破广场。
例子:
Input : 1 2 3
Output :1
We have a 3x3 square and we need
to make a rectangles of size 1x2.
So we need only square to cover the
rectangle.
Input : 11 23 14
Output :2
实际填充矩形的唯一方法是将每个正方形排列成与矩形的边平行。所以我们只需要找到正方形的数量来完全覆盖矩形的长度和宽度。
矩形的长度是l ,如果正方形的边长是l的除数,那么必须有l/a 个正方形来覆盖l 的全长。如果l不能被a整除,我们需要将l/a加1 ,以将其四舍五入。为此,我们可以使用ceil函数,因为 ceil(x) 返回大于或等于 x 的最小整数。
我们可以对矩形宽度做同样的事情,并将宽度上的正方形数量设为ceil(b/a) 。
因此,方格总数= ceil(m/a) * ceil(n/a) 。
C++
// C++ program to find the minimum number
// of squares to cover the surface of the
// rectangle with given dimensions
#include
using namespace std;
int squares(int l, int b, int a)
{
// function to count
// the number of squares that can
// cover the surface of the rectangle
return ceil(l / (double)a) * ceil(b / (double)a);
}
// Driver code
int main()
{
int l = 11, b = 23, a = 14;
cout << squares(l, b, a) << endl;
return 0;
}
Java
// Java program to find the minimum number
// of squares to cover the surface of the
// rectangle with given dimensions
class GFG
{
static int squares(int l, int b, int a)
{
// function to count
// the number of squares that can
// cover the surface of the rectangle
return (int)(Math.ceil(l / (double)a) *
Math.ceil(b / (double)a));
}
// Driver code
public static void main(String[] args)
{
int l = 11, b = 23, a = 14;
System.out.println(squares(l, b, a));
}
}
// This code is contributed by ChitraNayal
Python 3
# Python3 program to find the minimum number
# of squares to cover the surface of the
# rectangle with given dimensions
import math
def squares(l, b, a):
# function to count
# the number of squares that can
# cover the surface of the rectangle
return math.ceil(l / a) * math.ceil(b / a)
# Driver code
if __name__ == "__main__":
l = 11
b = 23
a = 14
print(squares(l, b, a))
# This code is contributed
# by ChitraNayal
C#
// C# program to find the minimum number
// of squares to cover the surface of the
// rectangle with given dimensions
using System;
class GFG
{
static int squares(int l, int b, int a)
{
// function to count
// the number of squares that can
// cover the surface of the rectangle
return (int)(Math.Ceiling(l / (double)a) *
Math.Ceiling(b / (double)a));
}
// Driver code
public static void Main()
{
int l = 11, b = 23, a = 14;
Console.Write(squares(l, b, a));
}
}
// This code is contributed by ChitraNayal
PHP
Javascript
输出:
2