给定两个整数 , 。找到大小为2 * 1的矩形数,可以将其放置在大小为n * m的矩形内。
笔记:
- No two small rectangles overlap.
- Each small rectangle lies entirely inside the large rectangle. It is allowed to touch the edges of the large rectangle.
例子:
Input : n = 3, m =3
Output : 4
Input : n = 2, m = 4
Output : 4
方法:
- 如果N是偶数,则放置M行N / 2个小矩形,并覆盖整个大矩形。
- 如果M为偶数,则放置N行M / 2个小矩形,并覆盖整个大矩形。
- 如果两者都为奇数,则用小矩形覆盖N – 1行的木板,并将floor(M / 2)小矩形置于最后一行。在最坏的情况下(N和M为奇数),一个单元仍未被发现。
下面是上述方法的实现:
C++
// CPP program to Find the number of
// rectangles of size 2*1 can be placed
// inside a rectangle of size n*m
#include
using namespace std;
// function to Find the number of
// rectangles of size 2*1 can be placed
// inside a rectangle of size n*m
int NumberOfRectangles(int n, int m)
{
// if n is even
if (n % 2 == 0)
return (n / 2) * m;
// if m is even
else if (m % 2 == 0)
return (m / 2) * n;
// if both are odd
return (n * m - 1) / 2;
}
// Driver code
int main()
{
int n = 3, m = 3;
// function call
cout << NumberOfRectangles(n, m);
return 0;
}
Java
// Java program to Find the number of
// rectangles of size 2*1 can be placed
// inside a rectangle of size n*m
public class GFG {
// function to Find the number of
// rectangles of size 2*1 can be placed
// inside a rectangle of size n*m
static int NumberOfRectangles(int n, int m)
{
// if n is even
if (n % 2 == 0)
return (n / 2) * m;
// if m is even
else if (m % 2 == 0)
return (m / 2) * n;
// if both are odd
return (n * m - 1) / 2;
}
public static void main(String args[])
{
int n = 3, m = 3;
// function call
System.out.println(NumberOfRectangles(n, m));
}
// This Code is contributed by ANKITRAI1
}
Python 3
# Python 3 program to Find the
# number of rectangles of size
# 2*1 can be placed inside a
# rectangle of size n*m
# function to Find the number
# of rectangles of size 2*1
# can be placed inside a
# rectangle of size n*m
def NumberOfRectangles(n, m):
# if n is even
if (n % 2 == 0):
return (n / 2) * m
# if m is even
elif (m % 2 == 0):
return (m // 2) * n
# if both are odd
return (n * m - 1) // 2
# Driver code
if __name__ == "__main__":
n = 3
m = 3
# function call
print(NumberOfRectangles(n, m))
# This code is contributed
# by ChitraNayal
C#
// C# program to Find the number of
// rectangles of size 2*1 can be placed
// inside a rectangle of size n*m
using System;
class GFG
{
// function to Find the number of
// rectangles of size 2*1 can be placed
// inside a rectangle of size n*m
static int NumberOfRectangles(int n, int m)
{
// if n is even
if (n % 2 == 0)
return (n / 2) * m;
// if m is even
else if (m % 2 == 0)
return (m / 2) * n;
// if both are odd
return (n * m - 1) / 2;
}
// Driver Code
public static void Main()
{
int n = 3, m = 3;
// function call
Console.WriteLine(NumberOfRectangles(n, m));
}
// This code is contributed
// by Akanksha Rai(Abby_akku)
}
PHP
输出:
4