📌  相关文章
📜  计算给定大小的矩形内可能的菱形数

📅  最后修改于: 2021-05-31 22:41:55             🧑  作者: Mango

给定一个高度H和宽度W的矩形,该矩形的左下角为(0,0) 。任务是计算具有满足以下条件的矩形内部或边界上所有点的不重复Rhombi数:

  • 具有非零面积。
  • 对角线平行于x和y轴。
  • 具有整数坐标。

例子:

方法:由于对角线与轴平行,让我们尝试固定对角线并在其上创建菱形。为了使菱形具有整数坐标,对角线的长度必须是偶数。让我们将对角线的长度固定为ij ,在矩形内以这些对角线长度可以形成的菱形数为(H – i + 1)*(W – j + 1) 。因此,我们遍历ij的所有可能值并更新计数。
下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Function to return the count of rhombi possible
long long countRhombi(int h, int w)
{
    long long ct = 0;
 
    // All possible diagonal lengths
    for (int i = 2; i <= h; i += 2)
        for (int j = 2; j <= w; j += 2)
 
            // Update rhombi possible with
            // the current diagonal lengths
            ct += (h - i + 1) * (w - j + 1);
 
    // Return the total count
    // of rhombi possible
    return ct;
}
 
// Driver code
int main()
{
    int h = 2, w = 2;
 
    cout << countRhombi(h, w);
 
    return 0;
}


Java
// Java implementation of the approach
import java.io.*;
 
class GFG
{
     
// Function to return the count of rhombi possible
static int countRhombi(int h, int w)
{
    int ct = 0;
 
    // All possible diagonal lengths
    for (int i = 2; i <= h; i += 2)
        for (int j = 2; j <= w; j += 2)
 
            // Update rhombi possible with
            // the current diagonal lengths
            ct += (h - i + 1) * (w - j + 1);
 
    // Return the total count
    // of rhombi possible
    return ct;
}
 
    // Driver code
    public static void main (String[] args)
    {
    int h = 2, w = 2;
    System.out.println (countRhombi(h, w));
    }
}
 
// This code is contributed by jit_t


Python 3
# Python 3 implementation of the approach
 
# Function to return the count of
# rhombi possible
def countRhombi(h, w):
 
    ct = 0;
 
    # All possible diagonal lengths
    for i in range(2, h + 1, 2):
        for j in range(2, w + 1, 2):
 
            # Update rhombi possible with
            # the current diagonal lengths
            ct += (h - i + 1) * (w - j + 1)
 
    # Return the total count
    # of rhombi possible
    return ct
 
# Driver code
if __name__ == "__main__":
 
    h = 2
    w = 2
 
    print(countRhombi(h, w))
 
# This code is contributed by ita_c


C#
// C# program to find the frequency of
// minimum element in the array
using System;
 
class GFG
{
         
    // Function to return the count
    // of rhombi possible
    static int countRhombi(int h, int w)
    {
        int ct = 0;
     
        // All possible diagonal lengths
        for (int i = 2; i <= h; i += 2)
            for (int j = 2; j <= w; j += 2)
     
                // Update rhombi possible with
                // the current diagonal lengths
                ct += (h - i + 1) * (w - j + 1);
     
        // Return the total count
        // of rhombi possible
        return ct;
    }
     
    // Driver code
    public static void Main()
    {
        int h = 2, w = 2;
         
        Console.WriteLine(countRhombi(h, w));
    }
}
 
// This code is contributed by Ryuga


PHP


Javascript


输出:
1

时间复杂度: O(H * W)

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。