以{X, Y, R}的形式给定三个圆A 、 B和C 的中心和半径,其中(X, Y)是圆的中心, R是该圆的半径。任务是检查是否有任何两个圆相交,使得第三个圆通过两个圆的交点和中心。如果发现是真的,则打印“是” 。否则,打印“否” 。
例子:
Input: A = {0, 0, 8}, B = {0, 10, 6}, C = {0, 5, 5}
Output: Yes
Input: arr[] = {{5, 0, 5}, {15, 0, 2}, {20, 0, 1}}
Output: No
方法:根据以下观察可以解决给定的问题:
- 假设C1C2 、 C1C3和C2C3分别是圆心C1和C2 、 C1和C3 、 C2和C3之间的距离。
- 如果C1C2 < r1 + r2,则圆C1和C2相交。
- 现在,由于第三个圆应该通过两个圆的中心和它们的交点,因此通过C1和C2的线成为弦S1S2 的垂直平分线,如图所示。
- 所以可以观察到,现在C1C3等于C3C2 it, C1C3是第三个圆的半径,第三个圆的中心是C1和C2的中点。
- 因此,当且仅当C3的中心成为C1和C2的中点,并且C1和C2相交且C3的半径成为C1C2 的一半时,给定的三个圆才满足给定的标准。
请按照以下步骤解决问题:
- 生成给定三个圆的每个组合并执行以下步骤:
- 找到前两个圆的中心之间的距离并将其存储在一个变量中,比如C1C2 。
- 如果C1C2小于前两个圆的半径之和,并且第三个圆的圆心是前两个圆的圆心的中点,则打印“Yes” 。
- 完成上述步骤后,如果不存在满足给定条件的任何圆圈组合,则打印“No” 。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Structure of the circle
class circle {
public:
double x;
double y;
double r;
};
//Utility function to check if given
// circles satisfy required criteria
bool check(circle C[])
{
// Stores the distance between
// the centres of C1 and C2
double C1C2
= sqrt((C[1].x - C[0].x)
* (C[1].x - C[0].x)
+ (C[1].y - C[0].y)
* (C[1].y - C[0].y));
// Stores the status if the given
// given criteria is satisfied or not
bool flag = 0;
// If C1C2 is less than the sum of
// the radii of the first 2 circles
if (C1C2 < (C[0].r + C[1].r)) {
// If C3 is the midpoint of
// the centres at C1 and C2
if ((C[0].x + C[1].x)
== 2 * C[2].x
&& (C[0].y + C[1].y)
== 2 * C[2].y) {
// Mark flag true
flag = 1;
}
}
// Return flag
return flag;
}
// Function to check if the given
// circles satisfy required criteria
bool IsFairTriplet(circle c[])
{
bool f = false;
// Check for the current
// combination of circles
f |= check(c);
for (int i = 0; i < 2; i++) {
swap(c[0], c[2]);
// Check for the next
// combination
f |= check(c);
}
return f;
}
// Driver Code
int main()
{
circle C[3];
C[0] = { 0, 0, 8 };
C[1] = { 0, 10, 6 };
C[2] = { 0, 5, 5 };
if (IsFairTriplet(C))
cout << "Yes";
else
cout << "No";
return 0;
}
Java
// Java approach for the above approach
class GFG{
// Structure of the circle
static class circle
{
double x;
double y;
double r;
public circle(int x, int y, int r)
{
this.x = x;
this.y = y;
this.r = r;
}
}
// Utility function to check if given
// circles satisfy required criteria
static boolean check(circle C[])
{
// Stores the distance between
// the centres of C1 and C2
double C1C2 = Math.sqrt(
(C[1].x - C[0].x) * (C[1].x - C[0].x) +
(C[1].y - C[0].y) * (C[1].y - C[0].y));
// Stores the status if the given
// given criteria is satisfied or not
boolean flag = false;
// If C1C2 is less than the sum of
// the radii of the first 2 circles
if (C1C2 < (C[0].r + C[1].r))
{
// If C3 is the midpoint of
// the centres at C1 and C2
if ((C[0].x + C[1].x) == 2 * C[2].x &&
(C[0].y + C[1].y) == 2 * C[2].y)
{
// Mark flag true
flag = true;
}
}
// Return flag
return flag;
}
// Function to check if the given
// circles satisfy required criteria
static boolean IsFairTriplet(circle c[])
{
boolean f = false;
// Check for the current
// combination of circles
f |= check(c);
for(int i = 0; i < 2; i++)
{
swap(c[0], c[2]);
// Check for the next
// combination
f |= check(c);
}
return f;
}
static void swap(circle circle1, circle circle2)
{
circle temp = circle1;
circle1 = circle2;
circle2 = temp;
}
// Driver Code
public static void main(String[] args)
{
circle C[] = new circle[3];
C[0] = new circle(0, 0, 8);
C[1] = new circle(0, 10, 6);
C[2] = new circle(0, 5, 5);
if (IsFairTriplet(C))
System.out.println("Yes");
else
System.out.println("No");
}
}
// This code is contributed by abhinavjain194
Python3
# Python3 program for the above approach
from math import sqrt
# Structure of the circle
class circle:
def __init__(self, a, b, c):
self.x = a
self.y = b
self.r = c
# Utility function to check if given
# circles satisfy required criteria
def check(C):
# Stores the distance between
# the centres of C1 and C2
C1C2 = sqrt((C[1].x - C[0].x)
* (C[1].x - C[0].x)
+ (C[1].y - C[0].y)
* (C[1].y - C[0].y))
# Stores the status if the given
# given criteria is satisfied or not
flag = 0
# If C1C2 is less than the sum of
# the radii of the first 2 circles
if (C1C2 < (C[0].r + C[1].r)):
# If C3 is the midpoint of
# the centres at C1 and C2
if ((C[0].x + C[1].x) == 2 * C[2].x and (C[0].y + C[1].y) == 2 * C[2].y):
# Mark flag true
flag = 1
# Return flag
return flag
# Function to check if the given
# circles satisfy required criteria
def IsFairTriplet(c):
f = False
# Check for the current
# combination of circles
f |= check(c)
for i in range(2):
c[0], c[2] = c[2], c[0]
# Check for the next
# combination
f |= check(c)
return f
# Driver Code
if __name__ == '__main__':
C = [circle(0,0,0) for i in range(3)]
C[0] = circle(0, 0, 8)
C[1] = circle(0, 10, 6)
C[2] = circle(0, 5, 5)
if (IsFairTriplet(C)):
print("Yes")
else:
print("No")
# This code is contributed by mohit kumar 29.
C#
// C# approach for the above approach
using System;
class GFG{
// Structure of the circle
class circle
{
public double x;
public double y;
public double r;
public circle(int x, int y, int r)
{
this.x = x;
this.y = y;
this.r = r;
}
}
// Utility function to check if given
// circles satisfy required criteria
static bool check(circle []C)
{
// Stores the distance between
// the centres of C1 and C2
double C1C2 = Math.Sqrt(
(C[1].x - C[0].x) * (C[1].x - C[0].x) +
(C[1].y - C[0].y) * (C[1].y - C[0].y));
// Stores the status if the given
// given criteria is satisfied or not
bool flag = false;
// If C1C2 is less than the sum of
// the radii of the first 2 circles
if (C1C2 < (C[0].r + C[1].r))
{
// If C3 is the midpoint of
// the centres at C1 and C2
if ((C[0].x + C[1].x) == 2 * C[2].x &&
(C[0].y + C[1].y) == 2 * C[2].y)
{
// Mark flag true
flag = true;
}
}
// Return flag
return flag;
}
// Function to check if the given
// circles satisfy required criteria
static bool IsFairTriplet(circle []c)
{
bool f = false;
// Check for the current
// combination of circles
f |= check(c);
for(int i = 0; i < 2; i++)
{
swap(c[0], c[2]);
// Check for the next
// combination
f |= check(c);
}
return f;
}
static void swap(circle circle1, circle circle2)
{
circle temp = circle1;
circle1 = circle2;
circle2 = temp;
}
// Driver Code
public static void Main(String[] args)
{
circle []C = new circle[3];
C[0] = new circle(0, 0, 8);
C[1] = new circle(0, 10, 6);
C[2] = new circle(0, 5, 5);
if (IsFairTriplet(C))
Console.WriteLine("Yes");
else
Console.WriteLine("No");
}
}
// This code is contributed by 29AjayKumar
Javascript
输出:
Yes
时间复杂度: O(1)
辅助空间: O(1)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。