📌  相关文章
📜  检查任何一对半圆是否相交

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

给定一个由N 个整数组成的数组arr[] ,每对点(x, y)将半圆的端点表示为(x, 0)(y, 0) ,任务是检查是否有任何半圆对相交与否。如果发现是真的,则打印“是” 。否则,打印“否”

例子:

方法:可以通过检查任何对半圆是否相交,然后相应地打印元素来解决给定的问题。请按照以下步骤解决问题:

  • 将所有可能的半圆及其对应的x 坐标y 坐标存储在一个向量中。
  • 现在,生成上述向量的所有可能对并执行以下步骤:
    • 让这对坐标为XY,并使用以下条件检查圆是否相交:
      • 如果(X[0] < Y[0] , X[1] < Y[1] and Y[0] < X[2])(Y[0] < X[0] , Y[1 ] ] < X[1]X[0] < Y[1]) ,那么半圆相交。否则,它不会相交。
      • 如果以上两个圆圈相交,则打印“Yes”并跳出循环。
  • 完成上述步骤后,如果任何一对圆不相交,则打印“No”
C++
// C++ program for the above approach
 
#include 
#include 
using namespace std;
 
// Function to check if any pairs of
// the semicircles intersects or not
bool checkIntersection(int arr[], int N)
{
 
    // Stores the coordinates of all
    // the semicircles
    vector > vec;
 
    for (int i = 0; i < N - 1; i++) {
 
        // x and y are coordinates
        int x = arr[i], y = arr[i + 1];
 
        // Store the minimum and maximum
        // value of the pair
        int minn = min(x, y);
        int maxx = max(x, y);
 
        // Push the pair in vector
        vec.push_back({ minn, maxx });
    }
 
    // Compare one pair with other pairs
    for (int i = 0; i < vec.size(); i++) {
 
        pair x = vec[i];
 
        // Generating the second pair
        for (int j = 0;
             j < vec.size(); j++) {
 
            // Extract all the second
            // pairs one by one
            pair y = vec[j];
 
            // 1st condition
            bool cond1
                = (x.first < y.first
                   and x.second < y.second
                   and y.first < x.second);
 
            // 2nd condition
            bool cond2
                = (y.first < x.first
                   and y.second < x.second
                   and x.first < y.second);
 
            // If any one condition
            // is true
            if (cond1 or cond2) {
                return true;
            }
        }
    }
 
    // If any pair of semicircles
    // doesn't exists
    return false;
}
 
// Driver Code
int main()
{
    int arr[] = { 0, 15, 5, 10 };
    int N = sizeof(arr) / sizeof(int);
 
    if (checkIntersection(arr, N))
        cout << "Yes";
    else
        cout << "No";
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG{
 
static class pair
{
    int first, second;
     
    public pair(int first, int second) 
    {
        this.first = first;
        this.second = second;
    }   
}
 
// Function to check if any pairs of
// the semicircles intersects or not
static boolean checkIntersection(int arr[], int N)
{
 
    // Stores the coordinates of all
    // the semicircles
    Vector vec = new Vector<>();
 
    for(int i = 0; i < N - 1; i++)
    {
         
        // x and y are coordinates
        int x = arr[i], y = arr[i + 1];
 
        // Store the minimum and maximum
        // value of the pair
        int minn = Math.min(x, y);
        int maxx = Math.max(x, y);
 
        // Push the pair in vector
        vec.add(new pair( minn, maxx ));
    }
 
    // Compare one pair with other pairs
    for(int i = 0; i < vec.size(); i++)
    {
        pair x = vec.elementAt(i);
 
        // Generating the second pair
        for(int j = 0;
                j < vec.size(); j++)
        {
 
            // Extract all the second
            // pairs one by one
            pair y = vec.elementAt(j);
 
            // 1st condition
            boolean cond1 = (x.first < y.first &&
                            x.second < y.second &&
                             y.first < x.second);
 
            // 2nd condition
            boolean cond2 = (y.first < x.first &&
                            y.second < x.second &&
                             x.first < y.second);
 
            // If any one condition
            // is true
            if (cond1 || cond2)
            {
                return true;
            }
        }
    }
 
    // If any pair of semicircles
    // doesn't exists
    return false;
}
 
// Driver Code
public static void main(String[] args)
{
    int arr[] = { 0, 15, 5, 10 };
    int N = arr.length;
 
    if (checkIntersection(arr, N))
        System.out.print("Yes");
    else
        System.out.print("No");
}
}
 
// This code is contributed by Amit Katiyar


Python3
# Python3 program for the above approach
 
# Function to check if any pairs of
# the semicircles intersects or not
def checkIntersection(arr, N):
     
    # Stores the coordinates of all
    # the semicircles
    vec = []
 
    for i in range(N - 1):
         
        # x and y are coordinates
        x = arr[i]
        y = arr[i + 1]
 
        # Store the minimum and maximum
        # value of the pair
        minn = min(x, y)
        maxx = max(x, y)
 
        # Push the pair in vector
        vec.append([minn, maxx])
 
    # Compare one pair with other pairs
    for i in range(len(vec)):
        x = vec[i]
 
        # Generating the second pair
        for j in range(len(vec)):
             
            # Extract all the second
            # pairs one by one
            y = vec[j]
 
            # 1st condition
            cond1 = (x[0] < y[0] and
                     x[1] < y[1] and
                     y[0] < x[1])
 
            # 2nd condition
            cond2 = (y[0] < x[0] and
                     y[1] < x[1] and
                     x[0] < y[1])
 
            # If any one condition
            # is true
            if (cond1 or cond2):
                return True
 
    # If any pair of semicircles
    # doesn't exists
    return False
 
# Driver Code
if __name__ == '__main__':
     
    arr = [ 0, 15, 5, 10 ]
    N = len(arr)
 
    if (checkIntersection(arr, N)):
        print("Yes")
    else:
        print("No")
 
# This code is contributed by ipg2016107


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
 
class GFG{
 
class pair
{
    public int first, second;
     
    public pair(int first, int second) 
    {
        this.first = first;
        this.second = second;
    }   
}
 
// Function to check if any pairs of
// the semicircles intersects or not
static bool checkIntersection(int []arr, int N)
{
 
    // Stores the coordinates of all
    // the semicircles
    List vec = new List();
 
    for(int i = 0; i < N - 1; i++)
    {
         
        // x and y are coordinates
        int x = arr[i], y = arr[i + 1];
 
        // Store the minimum and maximum
        // value of the pair
        int minn = Math.Min(x, y);
        int maxx = Math.Max(x, y);
 
        // Push the pair in vector
        vec.Add(new pair( minn, maxx ));
    }
 
    // Compare one pair with other pairs
    for(int i = 0; i < vec.Count; i++)
    {
        pair x = vec[i];
 
        // Generating the second pair
        for(int j = 0;
                j < vec.Count; j++)
        {
             
            // Extract all the second
            // pairs one by one
            pair y = vec[j];
 
            // 1st condition
            bool cond1 = (x.first < y.first &&
                         x.second < y.second &&
                          y.first < x.second);
 
            // 2nd condition
            bool cond2 = (y.first < x.first &&
                         y.second < x.second &&
                          x.first < y.second);
 
            // If any one condition
            // is true
            if (cond1 || cond2)
            {
                return true;
            }
        }
    }
 
    // If any pair of semicircles
    // doesn't exists
    return false;
}
 
// Driver Code
public static void Main(String[] args)
{
    int []arr = { 0, 15, 5, 10 };
    int N = arr.Length;
 
    if (checkIntersection(arr, N))
        Console.Write("Yes");
    else
        Console.Write("No");
}
}
 
// This code is contributed by Princi Singh


输出:
No

时间复杂度: O(N 2 )
辅助空间: O(N)

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