📜  最大化给定半径的半圆的值

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

给定一个半径为R半圆我们可以取圆周上的任何一点,让它成为P。现在,从该点P画两条线到直径的两侧。让线条为PQPS
任务是为给定的R找到表达式PS 2 + PQ的最大值

半圆

例子 :

Input : R = 1 
Output : 4.25  
(4*1^2 + 0.25) = 4.25

Input : R = 2
Output : 16.25 
(4 * 2^2 + 0.25)= 16.25

令 F = PS^2 + PQ。我们知道 QS = 2R(半圆的直径)
->我们也知道三角形 PQS永远是直角三角形,而不管点 P 在圆周上的位置

1.)QS^2 = PQ^2 + PS^2 (Pythagorean Theorem)

2.) Adding and Subtracting PQ on the RHS
     QS^2 = PQ^2 + PS^2 + PQ - PQ

3.) Since QS = 2R
   4*R^2 + PQ - PQ^2 = PS^2 + PQ 
=> 4*R^2 + PQ - PQ^2 = F

4.) Using the concept of maxima and minima 
differentiating F with respect to PQ and equating 
it to 0 to get the point of maxima for F i.e.
   1 - 2 * PQ = 0
 => PQ = 0.5

5.) Now F will be maximum at F = 4*R^2 + 0.25 
C++
// C++ program to find
// the maximum value of F
#include 
using namespace std;
 
// Function to find the
// maximum value of F
double maximumValueOfF (int R)
{
    // using the formula derived for
    // getting the maximum value of F
    return 4 * R * R + 0.25;    
}
     
// Drivers code
int main()
{
    int R = 3;
    printf("%.2f", maximumValueOfF(R));
    return 0;
}


JAVA
// JAVA program to find
// the maximum value of F
import java.io.*;
 
class GFG
{
     
    // Function to find the
    // maximum value of F
    static double maximumValueOfF (int R)
    {
         
        // using the formula derived for
        // getting the maximum value of F
        return 4 * R * R + 0.25;    
    }
     
    // Driver code
    public static void main (String[] args)
    {
        int R = 3;
        System.out.println(maximumValueOfF(R));
    }
         
}
 
// This code is contributed
// by anuj_67.


Python3
# python program to find
# the maximum value of F
 
# Function to find the
# maximum value of F
def maximumValueOfF (R):
     
    # using the formula derived for
    # getting the maximum value of F
    return 4 * R * R + 0.25
 
      
# Drivers code
R = 3
print(maximumValueOfF(R))
 
# This code is contributed by Sam007.


C#
// C# program to find the
// maximum value of F
using System;
 
class GFG
{
     
    // Function to find the
    // maximum value of F
    static double maximumValueOfF (int R)
    {
         
        // using the formula derived for
        // getting the maximum value of F
        return 4 * R * R + 0.25;    
    }
     
    // Driver code
    public static void Main ()
    {
        int R = 3;
        Console.WriteLine(maximumValueOfF(R));
    }
         
}
 
// This code is contributed by Sam007.


PHP


Javascript


输出 :
36.25

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