📌  相关文章
📜  计算满足给定条件的数组中的坐标数

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

给定一个由笛卡尔平面中的N 个坐标组成的数组arr[] ,任务是找到满足以下所有条件的坐标数,例如(X, Y)

  1. 所有可能的arr[i][0]必须小于X并且arr[i][1]必须等于Y
  2. 所有可能的arr[i][0]必须大于X并且arr[i][1]必须等于Y
  3. 所有可能的arr[i][0]必须小于Y并且arr[i][1]必须等于X
  4. 所有可能的arr[i][0]必须大于Y并且arr[i][1]必须等于X

例子:

方法:给定的问题可以通过考虑每个坐标来解决,比如(arr[i][0], arr[i][1])作为结果坐标,如果坐标(arr[i][0], arr [i][1])满足所有给定条件,然后计算当前坐标。检查所有坐标后,打印获得的总计数值。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to count the number of
// coordinates from a given set
// that satisfies the given conditions
int centralPoints(int arr[][2], int N)
{
    // Stores the count of central points
    int count = 0;
 
    // Store the count of
    // each x and y coordinates
    int c1, c2, c3, c4;
 
    // Find all possible pairs
    for (int i = 0; i < N; i++) {
 
        // Initialize variables c1, c2,
        // c3, c4 to define the status
        // of conditions
        c1 = 0, c2 = 0, c3 = 0;
        c4 = 0;
 
        // Stores value of each point
        int x = arr[i][0];
        int y = arr[i][1];
 
        // Check the conditions for
        // each point by generating
        // all possible pairs
        for (int j = 0; j < N; j++) {
 
            // If arr[j][0] > x and
            // arr[j][1] == y
            if (arr[j][0] > x
                && arr[j][1] == y) {
                c1 = 1;
            }
 
            // If arr[j][0] < x and
            // arr[j][1] == y
            if (arr[j][1] > y
                && arr[j][0] == x) {
                c2 = 1;
            }
 
            // If arr[j][1] > y and
            // arr[j][0] == x
            if (arr[j][0] < x
                && arr[j][1] == y) {
                c3 = 1;
            }
 
            // If arr[j][1] < y and
            // arr[j][0] == x
            if (arr[j][1] < y
                && arr[j][0] == x) {
                c4 = 1;
            }
        }
 
        // If all conditions satisfy
        // then point is central point
        if (c1 + c2 + c3 + c4 == 4) {
 
            // Increment the count by 1
            count++;
        }
    }
 
    // Return the count
    return count;
}
 
// Driver Code
int main()
{
    int arr[4][2] = { { 1, 0 }, { 2, 0 },
                      { 1, 1 }, { 1, -1 } };
    int N = sizeof(arr) / sizeof(arr[0]);
    cout << centralPoints(arr, N);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
 
public class GFG {
 
    // Function to count the number of
    // coordinates from a given set
    // that satisfies the given conditions
    static int centralPoints(int arr[][], int N)
    {
       
        // Stores the count of central points
        int count = 0;
 
        // Store the count of
        // each x and y coordinates
        int c1, c2, c3, c4;
 
        // Find all possible pairs
        for (int i = 0; i < N; i++) {
 
            // Initialize variables c1, c2,
            // c3, c4 to define the status
            // of conditions
            c1 = 0;
            c2 = 0;
            c3 = 0;
            c4 = 0;
 
            // Stores value of each point
            int x = arr[i][0];
            int y = arr[i][1];
 
            // Check the conditions for
            // each point by generating
            // all possible pairs
            for (int j = 0; j < N; j++) {
 
                // If arr[j][0] > x and
                // arr[j][1] == y
                if (arr[j][0] > x && arr[j][1] == y) {
                    c1 = 1;
                }
 
                // If arr[j][0] < x and
                // arr[j][1] == y
                if (arr[j][1] > y && arr[j][0] == x) {
                    c2 = 1;
                }
 
                // If arr[j][1] > y and
                // arr[j][0] == x
                if (arr[j][0] < x && arr[j][1] == y) {
                    c3 = 1;
                }
 
                // If arr[j][1] < y and
                // arr[j][0] == x
                if (arr[j][1] < y && arr[j][0] == x) {
                    c4 = 1;
                }
            }
 
            // If all conditions satisfy
            // then point is central point
            if (c1 + c2 + c3 + c4 == 4) {
 
                // Increment the count by 1
                count++;
            }
        }
 
        // Return the count
        return count;
    }
   
    // Driver Code
    public static void main(String[] args)
    {
 
        int arr[][]
            = { { 1, 0 }, { 2, 0 }, { 1, 1 }, { 1, -1 } };
        int N = arr.length;
        System.out.println(centralPoints(arr, N));
    }
}
 
// This code is contributed by Kingash.


Python3
# Python3 program for the above approach
 
# Function to count the number of
# coordinates from a given set
# that satisfies the given conditions
def centralPoints(arr, N):
     
    # Stores the count of central points
    count = 0
 
    # Find all possible pairs
    for i in range(N):
         
        # Initialize variables c1, c2,
        # c3, c4 to define the status
        # of conditions
        c1 = 0
        c2 = 0
        c3 = 0
        c4 = 0
 
        # Stores value of each point
        x = arr[i][0]
        y = arr[i][1]
 
        # Check the conditions for
        # each point by generating
        # all possible pairs
        for j in range(N):
             
            # If arr[j][0] > x and
            # arr[j][1] == y
            if (arr[j][0] > x and arr[j][1] == y):
                c1 = 1
 
            # If arr[j][0] < x and
            # arr[j][1] == y
            if (arr[j][1] > y and arr[j][0] == x):
                c2 = 1
 
            # If arr[j][1] > y and
            # arr[j][0] == x
            if (arr[j][0] < x and arr[j][1] == y):
                c3 = 1
 
            # If arr[j][1] < y and
            # arr[j][0] == x
            if (arr[j][1] < y and arr[j][0] == x):
                c4 = 1
 
        # If all conditions satisfy
        # then point is central point
        if (c1 + c2 + c3 + c4 == 4):
             
            # Increment the count by 1
            count += 1
 
    # Return the count
    return count
 
# Driver Code
if __name__ == '__main__':
     
    arr = [ [ 1, 0 ], [ 2, 0 ],
            [ 1, 1 ], [ 1, -1 ] ]
    N = len(arr)
     
    print(centralPoints(arr, N));
 
# This code is contributed by SURENDRA_GANGWAR


C#
// C# program for the above approach
using System;
 
class GFG{
 
// Function to count the number of
// coordinates from a given set
// that satisfies the given conditions
static int centralPoints(int[,] arr, int N)
{
     
    // Stores the count of central points
    int count = 0;
 
    // Store the count of
    // each x and y coordinates
    int c1, c2, c3, c4;
 
    // Find all possible pairs
    for(int i = 0; i < N; i++)
    {
         
        // Initialize variables c1, c2,
        // c3, c4 to define the status
        // of conditions
        c1 = 0;
        c2 = 0;
        c3 = 0;
        c4 = 0;
 
        // Stores value of each point
        int x = arr[i, 0];
        int y = arr[i, 1];
 
        // Check the conditions for
        // each point by generating
        // all possible pairs
        for(int j = 0; j < N; j++)
        {
             
            // If arr[j][0] > x and
            // arr[j][1] == y
            if (arr[j, 0] > x && arr[j, 1] == y)
            {
                c1 = 1;
            }
 
            // If arr[j][0] < x and
            // arr[j][1] == y
            if (arr[j, 1] > y && arr[j, 0] == x)
            {
                c2 = 1;
            }
 
            // If arr[j][1] > y and
            // arr[j][0] == x
            if (arr[j, 0] < x && arr[j, 1] == y)
            {
                c3 = 1;
            }
 
            // If arr[j][1] < y and
            // arr[j][0] == x
            if (arr[j, 1] < y && arr[j, 0] == x)
            {
                c4 = 1;
            }
        }
 
        // If all conditions satisfy
        // then point is central point
        if (c1 + c2 + c3 + c4 == 4)
        {
             
            // Increment the count by 1
            count++;
        }
    }
 
    // Return the count
    return count;
}
 
// Driver Code
public static void Main(string[] args)
{
    int[,] arr = { { 1, 0 }, { 2, 0 },
                   { 1, 1 }, { 1, -1 } };
    int N = arr.GetLength(0);
     
    Console.WriteLine(centralPoints(arr, N));
}
}
 
// This code is contributed by ukasp


Javascript


输出:
0

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

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