📌  相关文章
📜  计算点对之间的距离等于 K 维空间中的整数值

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

给定一个数组points[]表示K维空间中的N个点,任务是找到空间中点对的数量,使得每对点之间的距离是一个整数值。

例子:

方法:想法是生成给定数组的所有可能对,并找到每对点之间的距离并检查它是否为整数值。如果发现为真,则增加计数。最后,打印获得的总数。请按照以下步骤解决问题:

  • 点对之间的距离({ a 1 , a 2 , …, a K }, { b 1 , b 2 , …, b K }) 可以使用以下公式计算:
  • 遍历数组,并生成给定数组的所有可能对。
  • 对于每对点,检查该对点之间的距离是否为整数。如果发现为真,则增加计数。
  • 最后,打印获得的计数。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to  find pairs whose distance between
// the points of is an integer value.
void cntPairs(vector > points, int n, int K)
{
 
    // Stores count of pairs whose distance
    // between points is an integer
    int ans = 0;
 
    // Traverse the array, points[]
    for (int i = 0; i < n; i++) {
 
        for (int j = i + 1; j < n; j++) {
 
            // Stores distance between
            // points(i, j)
            int dist = 0;
 
            // Traverse all the points of
            // current pair
            for (int k = 0; k < K; k++) {
 
                // Update temp
                int temp = (points[i][k]
                            - points[j][k]);
 
                // Update dist
                dist += temp * temp;
            }
 
            // If dist is a perfect square
            if (sqrt(dist) * sqrt(dist) == dist) {
 
                // Update ans
                ans += 1;
            }
        }
    }
 
    cout << ans << endl;
}
 
// Driver Code
int main()
{
 
    // Given value of K
    int K = 2;
 
    // Given points
    vector > points
        = { { 1, 2 }, { 5, 5 }, { -2, 8 } };
 
    // Given value of N
    int n = points.size();
 
    // Function Call
    cntPairs(points, n, K);
 
    return 0;
}


Java
// Java program for the above approach
class GFG
{
 
  // Function to  find pairs whose distance between
  // the points of is an integer value.
  static void cntPairs(int [][]points, int n, int K)
  {
 
    // Stores count of pairs whose distance
    // between points is an integer
    int ans = 0;
 
    // Traverse the array, points[]
    for (int i = 0; i < n; i++)
    {
      for (int j = i + 1; j < n; j++)
      {
 
        // Stores distance between
        // points(i, j)
        int dist = 0;
 
        // Traverse all the points of
        // current pair
        for (int k = 0; k < K; k++)
        {
 
          // Update temp
          int temp = (points[i][k]
                      - points[j][k]);
 
          // Update dist
          dist += temp * temp;
        }
 
        // If dist is a perfect square
        if (Math.sqrt(dist) * Math.sqrt(dist) == dist)
        {
 
          // Update ans
          ans += 1;
        }
      }
    }
    System.out.print(ans +"\n");
  }
 
  // Driver Code
  public static void main(String[] args)
  {
 
    // Given value of K
    int K = 2;
 
    // Given points
    int [][]points
      = { { 1, 2 }, { 5, 5 }, { -2, 8 } };
 
    // Given value of N
    int n = points.length;
 
    // Function Call
    cntPairs(points, n, K);
  }
}
 
// This code is contributed by shikhasingrajput


Python3
# Python program for the above approach
 
# Function to  find pairs whose distance between
# the points of is an integer value.
def cntPairs(points, n, K):
   
    # Stores count of pairs whose distance
    # between points is an integer
    ans = 0
 
    # Traverse the array, points[]
    for i in range(0, n):
 
        for j in range(i + 1, n):
 
            # Stores distance between
            # points(i, j)
            dist = 0
 
            # Traverse all the points of
            # current pair
            for k in range(K):
 
                # Update temp
                temp = (points[i][k] - points[j][k])
 
                # Update dist
                dist += temp * temp
 
            # If dist is a perfect square
            if (((dist)**(1/2)) * ((dist)**(1/2)) == dist):
 
                # Update ans
                ans += 1
    print(ans)
 
# Driver Code
# Given value of K
K = 2
 
# Given points
points = [ [ 1, 2 ], [ 5, 5 ], [ -2, 8 ]]
 
# Given value of N
n = len(points)
 
# Function Call
cntPairs(points, n, K)
 
# This code is contributed by rohitsingh07052.


C#
// C# program for the above approach
using System;
class GFG
{
 
  // Function to  find pairs whose distance between
  // the points of is an integer value.
  static void cntPairs(int[, ] points, int n, int K)
  {
 
    // Stores count of pairs whose distance
    // between points is an integer
    int ans = 0;
 
    // Traverse the array, points[]
    for (int i = 0; i < n; i++) {
 
      for (int j = i + 1; j < n; j++) {
 
        // Stores distance between
        // points(i, j)
        int dist = 0;
 
        // Traverse all the points of
        // current pair
        for (int k = 0; k < K; k++) {
 
          // Update temp
          int temp
            = (points[i, k] - points[j, k]);
 
          // Update dist
          dist += temp * temp;
        }
 
        // If dist is a perfect square
        if (Math.Sqrt(dist) * Math.Sqrt(dist)
            == dist) {
 
          // Update ans
          ans += 1;
        }
      }
    }
 
    Console.WriteLine(ans);
  }
 
  // Driver Code
  public static void Main()
  {
 
    // Given value of K
    int K = 2;
 
    // Given points
    int[, ] points = { { 1, 2 }, { 5, 5 }, { -2, 8 } };
 
    // Given value of N
    int n = points.GetLength(0);
 
    // Function Call
    cntPairs(points, n, K);
  }
}
 
// This code is contributed by chitranayal.


Javascript


输出:
1

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

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