Given a rectangle with length l and breadth b, we need to find the minimum number of squares that can cover the surface of the rectangle, given that each square has a side of length a. It is allowed to cover the surface larger than the rectangle, but the rectangle has to be covered. It is not allowed to break the square. Examples:
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
The only way to actually fill the rectangle optimally is to arrange each square such that it is parallel to the sides of the rectangle.So we just need to find the number of squares to fully cover the length and breadth of the rectangle. The length of the rectangle is l, and if the side length of the square is a divides l, then there must be l/a squares to cover the full length of l. If l isn’t divisible by a, we need to add 1 to l/a, to round it down.For this we can use the ceil function, as ceil(x) returns the least integer which is above or equal to x. We can do the same with the rectangle width, and take the number of squares across the width to be ceil(b/a). So, total number of squares=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 <bits/stdc++.h>
usingnamespacestd;
intsquares(intl, intb, inta)
{
// function to count
// the number of squares that can
// cover the surface of the rectangle
returnceil(l / (double)a) * ceil(b / (double)a);
}
// Driver code
intmain()
{
intl = 11, b = 23, a = 14;
cout << squares(l, b, a) << endl;
return0;
}
Java
// Java program to find the minimum number
// of squares to cover the surface of the
// rectangle with given dimensions
classGFG
{
staticintsquares(intl, intb, inta)
{
// 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
publicstaticvoidmain(String[] args)
{
intl = 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
importmath
defsquares(l, b, a):
# function to count
# the number of squares that can
# cover the surface of the rectangle
returnmath.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
usingSystem;
classGFG
{
staticintsquares(intl, intb, inta)
{
// 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
publicstaticvoidMain()
{
intl = 11, b = 23, a = 14;
Console.Write(squares(l, b, a));
}
}
// This code is contributed by ChitraNayal
PHP
<?php
// PHP program to find the minimum number
// of squares to cover the surface of the
// rectangle with given dimensions
functionsquares($l, $b, $a)
{
// function to count
// the number of squares that can
// cover the surface of the rectangle
returnceil($l/ (double)$a) *
ceil($b/ (double)$a);
}
// Driver code
$l= 11;
$b= 23;
$a= 14;
echosquares($l, $b, $a);
// This code is contributed
// by ChitraNayal
?>
Javascript
<script>
// javascript program to find the minimum number
// of squares to cover the surface of the
// rectangle with given dimensions
functionsquares(l , b , a)
{
// function to count
// the number of squares that can
// cover the surface of the rectangle
returnparseInt(Math.ceil(l / a) *
Math.ceil(b / a));
}
// Driver code
varl = 11, b = 23, a = 14;
document.write(squares(l, b, a));
// This code is contributed by Amit Katiyar
</script>
Output:
2
Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy