📜  可以刻在矩形中的最大可能菱形的区域

📅  最后修改于: 2021-04-29 13:01:43             🧑  作者: Mango

给定一个长度为l且宽度为b的矩形,任务是找到可以刻在矩形中的最大菱形。
例子

Input : l = 5, b = 4
Output : 10

Input : l = 16, b = 6
Output : 48

从图中可以看出,可以在矩形内刻出的最大菱形的对角线将等于矩形的长度和宽度。
因此,菱形面积A =(l * b)/ 2
下面是上述方法的实现:

C++
// C++ Program to find the biggest rhombus
// which can be inscribed within the rectangle
#include 
using namespace std;
 
// Function to find the area
// of the biggest rhombus
float rhombusarea(float l, float b)
{
    // the length and breadth cannot be negative
    if (l < 0 || b < 0)
        return -1;
 
    // area of the rhombus
    return (l * b) / 2;
}
 
// Driver code
int main()
{
    float l = 16, b = 6;
    cout << rhombusarea(l, b) << endl;
    return 0;
}


Java
// Java Program to find the
// biggest rhombus which can be
// inscribed within the rectangle
import java.io.*;
 
class GFG
{
 
// Function to find the area
// of the biggest rhombus
static float rhombusarea(float l,
                         float b)
{
    // the length and breadth
    // cannot be negative
    if (l < 0 || b < 0)
        return -1;
 
    // area of the rhombus
    return (l * b) / 2;
}
 
// Driver code
public static void main (String[] args)
{
    float l = 16, b = 6;
    System.out.println(rhombusarea(l, b));
}
}
 
// This code is contributed
// by inder_verma


Python3
# Python 3 Program to find the biggest rhombus
# which can be inscribed within the rectangle
 
 
# Function to find the area
# of the biggest rhombus
def rhombusarea(l,b):
    # the length and breadth cannot be negative
    if (l < 0 or b < 0):
        return -1
 
    # area of the rhombus
    return (l * b) / 2
 
# Driver code
if __name__ == '__main__':
    l = 16
    b = 6
    print(rhombusarea(l, b))


C#
// C# Program to find the
// biggest rhombus which can be
// inscribed within the rectangle
using System;
 
class GFG
{
 
// Function to find the area
// of the biggest rhombus
static float rhombusarea(float l,
                        float b)
{
    // the length and breadth
    // cannot be negative
    if (l < 0 || b < 0)
        return -1;
 
    // area of the rhombus
    return (l * b) / 2;
}
 
// Driver code
public static void Main ()
{
    float l = 16, b = 6;
    Console.WriteLine(rhombusarea(l, b));
}
}
 
// This code is contributed
// by shs


PHP


Javascript


输出:
48