📌  相关文章
📜  内接于等边三角形的不同矩形的数量

📅  最后修改于: 2021-10-23 08:04:13             🧑  作者: Mango

给定一个由点 ( . ) 组成的等边三角形,它们连接在一起形成三角形和一个整数n ,它表示三角形的边长。任务是计算可以在给定三角形中内切的矩形数量,使得:

  1. 水平边必须平行于给定三角形的底边。
  2. 矩形只能通过连接点形成,即矩形的所有四个边都必须接触三角形上的点。
  3. 只应计算不同的矩形。

例子:

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

处理方法:从上图可以看出, nn-1层对构成任何矩形没有贡献,所以我们从n-2层开始计算矩形。当n是奇数并且我们计算的级别说i也是奇数时,差将是偶数,因此我们将其除以 2。这将给我们可以使用的级别ni之间的垂直级别数用于制作矩形,如果两者都是偶数,则这是相同的,因为偶数的差是偶数。
但是,当其中一个是奇数时,差异将是奇数,因此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
?>


Javascript


输出:
11

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程