给定一个矩形,该矩形的长度为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,并且如果正方形的边长为a整除升,那么必须有L / A正方形覆盖升的全长。如果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