📜  查找正方形内刻有八边形的一面的程序

📅  最后修改于: 2021-04-22 08:03:16             🧑  作者: Mango

给定边长“ a”的平方,任务是找到可以内切的最大八边形的边长。
例子:

Input: a = 4
Output: 1.65685

Input: a = 5
Output: 2.07107

方法

下面是上述方法的实现:

C++
// C++ Program to find the side of the octagon
// which can be inscribed within the square
 
#include 
using namespace std;
 
// Function to find the side
// of the octagon
float octaside(float a)
{
 
    // side cannot be negative
    if (a < 0)
        return -1;
 
    // side of the octagon
    float s = a / (sqrt(2) + 1);
    return s;
}
 
// Driver code
int main()
{
 
    // Get he square side
    float a = 4;
 
    // Find the side length of the square
    cout << octaside(a) << endl;
 
    return 0;
}


Java
// Java Program to find the side of the octagon
// which can be inscribed within the square
 
import java.io.*;
 
class GFG {
     
// Function to find the side
// of the octagon
static double octaside(double a)
{
 
    // side cannot be negative
    if (a < 0)
        return -1;
 
    // side of the octagon
    double s = a / (Math.sqrt(2) + 1);
    return s;
}
 
// Driver code
     
    public static void main (String[] args) {
         
    // Get he square side
    double a = 4;
 
    // Find the side length of the square
    System.out.println( octaside(a));
 
         
         
    }
}
//This Code  is contributed by ajit


Python3
# Python 3 Program to find the side
# of the octagon which can be
# inscribed within the square
from math import sqrt
 
# Function to find the side
# of the octagon
def octaside(a):
     
    # side cannot be negative
    if a < 0:
        return -1
 
    # side of the octagon
    s = a / (sqrt(2) + 1)
    return s
 
# Driver code
if __name__ == '__main__':
     
    # Get he square side
    a = 4
 
    # Find the side length of the square
    print("{0:.6}".format(octaside(a)))
     
# This code is contributed
# by Surendra_Gangwar


C#
// C# Program to find the side
// of the octagon which can be
// inscribed within the square
using System;
 
class GFG
{
     
// Function to find the side
// of the octagon
static double octaside(double a)
{
 
    // side cannot be negative
    if (a < 0)
        return -1;
 
    // side of the octagon
    double s = a / (Math.Sqrt(2) + 1);
    return s;
}
 
// Driver code
static public void Main ()
{
    // Get he square side
    double a = 4;
     
    // Find the side length
    // of the square
    Console.WriteLine( octaside(a));
}
}
 
// This code is contributed
// by akt_mit


PHP


Javascript


输出:
1.65685