给定一个数组points[]表示K维空间中的N个点,任务是找到空间中点对的数量,使得每对点之间的距离是一个整数值。
例子:
Input: points[] = { {1, 2}, {5, 5}, {2, 8} }, K = 2
Output: 1
Explanation:
Distance between points of the pair(points[0], points[1]) = 5
Distance between points of the pair(points[1], points[2]) = sqrt(58)
Distance between points of the pair(points[0], points[2]) = 3 * sqrt(5)
Therefore, the required output is 1.
Input: points[] = { {-3, 7, 8, 2}, {-12, 1, 10, 2}, {-2, 8, 9, 3} }, K = 4
Output: 2.
方法:想法是生成给定数组的所有可能对,并找到每对点之间的距离并检查它是否为整数值。如果发现为真,则增加计数。最后,打印获得的总数。请按照以下步骤解决问题:
- 点对之间的距离({ a 1 , a 2 , …, a K }, { b 1 , b 2 , …, b K }) 可以使用以下公式计算:
Distance(a, b) = sqrt(((a1 – b1)2 + (a2 – b2)2 + …. + (aK – bK)2 ))
- 遍历数组,并生成给定数组的所有可能对。
- 对于每对点,检查该对点之间的距离是否为整数。如果发现为真,则增加计数。
- 最后,打印获得的计数。
下面是上述方法的实现:
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 现场工作专业课程和学生竞争性编程现场课程。