给定一个由点( 。 )组成的等边三角形,它们连接在一起形成三角形,而整数n表示三角形边的长度。任务是计算可以在给定三角形中内接的矩形的数量,以便:
- 水平边缘必须平行于给定三角形的底边。
- 矩形只能通过连接点来形成,即矩形的所有四个边缘都必须接触三角形上的点。
- 仅应计算不同的矩形。
例子:
Input: N = 3
Output: 1
.
. .
. . .
. . . .
The only triangle possible has the top edges
at the two points of the second row and bottom edges
at the 2nd and the 3rd points in the last row.
Input: N = 5
Output: 11
方法:从上图中可以明显看出, n和n – 1级对创建任何矩形都没有帮助,因此我们从n – 2级开始计算矩形。当n为奇数并且我们计算的水平也为i时,则差将为偶数,因此我们将其除以2。这将为我们提供水平n和i之间可以使用的垂直水平数用于制作矩形,如果两个都是偶数,则这是相同的,因为偶数的差是偶数。
但是,当其中之一为奇数时,差将为奇数,因此n – 1电平将有助于选择垂直电平,因此在计算中将使用n – 1电平。为了计算水平方向上选择两个点的方式数量,我们可以使用n个自然数之和的公式,因为N C 2 = 1 + 2 + 3 +…+(N – 1) 。现在,我们将在一个级别中选择两个点的方法数量乘以垂直级别中的点数量。这将是我们针对特定级别的结果,因此我们将重复这些步骤直到最后并总结所有值。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
// Function to return the count of
// rectangles when n is odd
int countOdd(int n)
{
int coun = 0, m, j, i;
for (i = n - 2; i >= 1; i--) {
if (i & 1) {
// Calculating number of dots
// in vertical level
m = (n - i) / 2;
// Calculating number of ways
// to select two points in the
// horizontal level i
j = (i * (i + 1)) / 2;
// Multiply both to obtain the number of
// rectangles formed at that level
coun += j * m;
}
else {
// Calculating number of dots
// in vertical level
m = ((n - 1) - i) / 2;
// Calculating number of ways
// to select two points in the
// horizontal level i
j = (i * (i + 1)) / 2;
// Multiply both to obtain the number of
// rectangles formed at that level
coun += j * m;
}
}
return coun;
}
// Function to return the count of
// rectangles when n is even
int countEven(int n)
{
int coun = 0, m, j, i;
for (i = n - 2; i >= 1; i--) {
if (i & 1) {
m = ((n - 1) - i) / 2;
j = (i * (i + 1)) / 2;
coun += j * m;
}
else {
m = (n - i) / 2;
j = (i * (i + 1)) / 2;
coun += j * m;
}
}
return coun;
}
// Driver code
int main()
{
int n = 5;
// If n is odd
if (n & 1)
cout << countOdd(n);
else
cout << countEven(n);
return 0;
}
Java
// Java implementation of the approach
import java.io.*;
class GFG {
// Function to return the count of
// rectangles when n is odd
static int countOdd(int n)
{
int coun = 0, m, j, i;
for (i = n - 2; i >= 1; i--) {
if (i >= 1) {
// Calculating number of dots
// in vertical level
m = (n - i) / 2;
// Calculating number of ways
// to select two points in the
// horizontal level i
j = (i * (i + 1)) / 2;
// Multiply both to obtain the number of
// rectangles formed at that level
coun += j * m;
}
else {
// Calculating number of dots
// in vertical level
m = ((n - 1) - i) / 2;
// Calculating number of ways
// to select two points in the
// horizontal level i
j = (i * (i + 1)) / 2;
// Multiply both to obtain the number of
// rectangles formed at that level
coun += j * m;
}
}
return coun;
}
// Function to return the count of
// rectangles when n is even
static int countEven(int n)
{
int coun = 0, m, j, i;
for (i = n - 2; i >= 1; i--) {
if (i >= 1) {
m = ((n - 1) - i) / 2;
j = (i * (i + 1)) / 2;
coun += j * m;
}
else {
m = (n - i) / 2;
j = (i * (i + 1)) / 2;
coun += j * m;
}
}
return coun;
}
// Driver code
public static void main(String[] args)
{
int n = 5;
// If n is odd
if (n >= 1)
System.out.println(countOdd(n));
else
System.out.println(countEven(n));
}
}
// This code is contributed by Tushil..
Python3
# Python 3 implementation of the approach
# Function to return the count of
# rectangles when n is odd
def countOdd(n):
coun = 0
i = n - 2
while (i >= 1):
if (i & 1):
# Calculating number of dots
# in vertical level
m = int((n - i) / 2)
# Calculating number of ways
# to select two points in the
# horizontal level i
j = int((i * (i + 1)) / 2)
# Multiply both to obtain the number of
# rectangles formed at that level
coun += j * m
else:
# Calculating number of dots
# in vertical level
m = int(((n - 1) - i) / 2)
# Calculating number of ways
# to select two points in the
# horizontal level i
j = int((i * (i + 1)) / 2)
# Multiply both to obtain the number of
# rectangles formed at that level
coun += j * m
i -= 1
return coun
# Function to return the count of
# rectangles when n is even
def countEven(n):
coun = 0
i = n - 2
while(i >= 1):
if (i & 1):
m = int(((n - 1) - i) / 2)
j = int((i * (i + 1)) / 2)
coun += j * m
else:
m = int((n - i) / 2)
j = (i * (i + 1)) // 2
coun += j * m
return coun
# Driver code
if __name__ == '__main__':
n = 5
# If n is odd
if (n & 1):
print(countOdd(n))
else:
print(countEven(n))
# This code is contributed by
# Surendra_Gangwar
C#
// C# implementation of the approach
using System;
class GFG {
// Function to return the count of
// rectangles when n is odd
static int countOdd(int n)
{
int coun = 0, m, j, i;
for (i = n - 2; i >= 1; i--) {
if (i >= 1) {
// Calculating number of dots
// in vertical level
m = (n - i) / 2;
// Calculating number of ways
// to select two points in the
// horizontal level i
j = (i * (i + 1)) / 2;
// Multiply both to obtain the number of
// rectangles formed at that level
coun += j * m;
}
else {
// Calculating number of dots
// in vertical level
m = ((n - 1) - i) / 2;
// Calculating number of ways
// to select two points in the
// horizontal level i
j = (i * (i + 1)) / 2;
// Multiply both to obtain the number of
// rectangles formed at that level
coun += j * m;
}
}
return coun;
}
// Function to return the count of
// rectangles when n is even
static int countEven(int n)
{
int coun = 0, m, j, i;
for (i = n - 2; i >= 1; i--) {
if (i >= 1) {
m = ((n - 1) - i) / 2;
j = (i * (i + 1)) / 2;
coun += j * m;
}
else {
m = (n - i) / 2;
j = (i * (i + 1)) / 2;
coun += j * m;
}
}
return coun;
}
// Driver code
static public void Main()
{
int n = 5;
// If n is odd
if (n >= 1)
Console.Write(countOdd(n));
else
Console.Write(countEven(n));
}
}
// This code is contributed by Tushil..
PHP
= 1; $i--)
{
if ($i & 1)
{
// Calculating number of dots
// in vertical level
$m = ($n - $i) / 2;
// Calculating number of ways
// to select two points in the
// horizontal level i
$j = ($i * ($i + 1)) / 2;
// Multiply both to obtain the number of
// rectangles formed at that level
$coun += $j * $m;
}
else
{
// Calculating number of dots
// in vertical level
$m = (($n - 1) - $i) / 2;
// Calculating number of ways
// to select two points in the
// horizontal level i
$j = ($i * ($i + 1)) / 2;
// Multiply both to obtain the number of
// rectangles formed at that level
$coun += $j * $m;
}
}
return $coun;
}
// Function to return the count of
// rectangles when n is even
function countEven($n)
{
$coun = 0;
for ($i = $n - 2; $i >= 1; $i--)
{
if ($i & 1)
{
$m = (($n - 1) - i) / 2;
$j = ($i * ($i + 1)) / 2;
$coun += $j * $m;
}
else
{
$m = ($n - $i) / 2;
$j = ($i * ($i + 1)) / 2;
$coun += $j * $m;
}
}
return $coun;
}
// Driver code
$n = 5;
// If n is odd
if ($n & 1)
echo countOdd($n);
else
echo countEven($n);
// This code is contributed by Arnab Kundu
?>
输出:
11