给定a,b和c的系数X 2 , X和抛物线方程中的常数项 ,任务是找到抛物线的Latus直肠的长度。
例子:
Input: a = 3, b = 5, c = 1
Output: 0.333333
Input: a = 4, b = 0, c = 4
Output: 0.25
方法:可以根据以下观察结果解决给定问题:
Observation:
- The Latus rectum of a parabola is the perpendicular line to the axis and at the focus of parabola and its length is equal to 4 times the distance between the focus and vertex of the parabola.
- Therefore, the task is reduced to find the distance between focus and vertex of the parabola using formula:
请按照以下步骤解决问题:
- 初始化两个变量,例如顶点和焦点,以存储抛物线的顶点和焦点的坐标。
- 找到顶点的坐标和抛物线的焦点,并将其存储在相应的变量中。
- 初始化一个变量,例如length ,并将其设置为抛物线的顶点和焦点之间的距离的4倍。
- 打印长度值作为答案。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to calculate distance
// between two points
float distance(float x1, float y1,
float x2, float y2)
{
// Calculating distance
return sqrt((x2 - x1) * (x2 - x1)
+ (y2 - y1) * (y2 - y1));
}
// Function to calculate length of
// the latus rectum of a parabola
float lengthOfLatusRectum(float a,
float b, float c)
{
// Stores the co-ordinates of
// the vertex of the parabola
pair vertex
= { (-b / (2 * a)),
(((4 * a * c) - (b * b)) / (4 * a)) };
// Stores the co-ordinates of
// the focus of parabola
pair focus
= { (-b / (2 * a)),
(((4 * a * c) - (b * b) + 1) / (4 * a)) };
// Print the distance between focus and vertex
cout << 4 * distance(
focus.first, focus.second,
vertex.first, vertex.second);
}
// Driver Code
int main()
{
// Given a, b & c
float a = 3, b = 5, c = 1;
// Function call
lengthOfLatusRectum(a, b, c);
return 0;
}
Java
// Java program for the above approach
class GFG{
static class pair
{
float first;
float second;
public pair(float first, float second)
{
this.first = first;
this.second = second;
}
}
// Function to calculate distance
// between two points
static float distance(float x1, float y1,
float x2, float y2)
{
// Calculating distance
return (float) Math.sqrt((x2 - x1) * (x2 - x1)
+ (y2 - y1) * (y2 - y1));
}
// Function to calculate length of
// the latus rectum of a parabola
static void lengthOfLatusRectum(float a,
float b, float c)
{
// Stores the co-ordinates of
// the vertex of the parabola
pair vertex
= new pair( (-b / (2 * a)),
(((4 * a * c) - (b * b)) / (4 * a)) );
// Stores the co-ordinates of
// the focus of parabola
pair focus
= new pair( (-b / (2 * a)),
(((4 * a * c) - (b * b) + 1) / (4 * a)) );
// Print the distance between focus and vertex
System.out.print(4 * distance(
(float)focus.first, (float)focus.second,
(float)vertex.first, (float)vertex.second));
}
// Driver Code
public static void main(String[] args)
{
// Given a, b & c
float a = 3, b = 5, c = 1;
// Function call
lengthOfLatusRectum(a, b, c);
}
}
// This code is contributed by 29AjayKumar
Python3
# Python 3 program for the above approach
from math import sqrt
# Function to calculate distance
# between two points
def distance(x1, y1, x2, y2):
# Calculating distance
return sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1))
# Function to calculate length of
# the latus rectum of a parabola
def lengthOfLatusRectum(a, b, c):
# Stores the co-ordinates of
# the vertex of the parabola
vertex = [(-b / (2 * a)), (((4 * a * c) - (b * b)) / (4 * a))]
# Stores the co-ordinates of
# the focus of parabola
focus = [(-b / (2 * a)), (((4 * a * c) - (b * b) + 1) / (4 * a))]
# Print the distance between focus and vertex
print("{:.6f}".format(4 * distance(focus[0], focus[1], vertex[0], vertex[1])))
# Driver Code
if __name__ == "__main__":
# Given a, b & c
a = 3
b = 5
c = 1
# Function call
lengthOfLatusRectum(a, b, c)
# This code is contributed by bgangwar59.
C#
// C# program for the above approach
using System;
public class GFG{
class pair
{
public float first;
public float second;
public pair(float first, float second)
{
this.first = first;
this.second = second;
}
}
// Function to calculate distance
// between two points
static float distance(float x1, float y1,
float x2, float y2)
{
// Calculating distance
return (float) Math.Sqrt((x2 - x1) * (x2 - x1)
+ (y2 - y1) * (y2 - y1));
}
// Function to calculate length of
// the latus rectum of a parabola
static void lengthOfLatusRectum(float a,
float b, float c)
{
// Stores the co-ordinates of
// the vertex of the parabola
pair vertex
= new pair( (-b / (2 * a)),
(((4 * a * c) - (b * b)) / (4 * a)) );
// Stores the co-ordinates of
// the focus of parabola
pair focus
= new pair( (-b / (2 * a)),
(((4 * a * c) - (b * b) + 1) / (4 * a)) );
// Print the distance between focus and vertex
Console.Write(4 * distance(
(float)focus.first, (float)focus.second,
(float)vertex.first, (float)vertex.second));
}
// Driver Code
public static void Main(String[] args)
{
// Given a, b & c
float a = 3, b = 5, c = 1;
// Function call
lengthOfLatusRectum(a, b, c);
}
}
// This code is contributed by 29AjayKumar
输出:
0.333333
时间复杂度: O(1)
辅助空间: O(1)