📜  三胞胎数

📅  最后修改于: 2021-10-27 07:14:21             🧑  作者: Mango

给定平面中二维数组形式的N个点,每行由两个整数LR组成,其中L属于 x 坐标, R属于 y 坐标。任务是计算点的三元组(比如a, b & c) ,使得a & b之间的距离等于a & c之间的距离。
注意:三胞胎的顺序很重要。

例子:

方法:

  1. 对于每个点,计算它与其他点的距离。
  2. 存储点到地图中其他点的中间距离(比如d )。
  3. 如果 Map 已经具有相同的距离,则三元组的数量是 Map 中为d存储的值的两倍。
  4. 更新地图中当前距离的计数。

下面是上面的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to count the triplets
int countTriplets(vector >& p)
{
 
    // Initialise count
    int count = 0;
 
    // Traverse the arr[]
    for (int i = 0; i < p.size(); i++) {
 
        // Map to store the distance between
        // every pairs p[i] and p[j]
        unordered_map d;
 
        for (int j = 0; j < p.size(); j++) {
 
            // Find the distance
            int dist = pow(p[j][1] - p[i][1], 2)
                       + pow(p[j][0] - p[i][0], 2);
 
            // If count of distance is greater
            // than 0, then find the count
            if (d[dist] > 0) {
                count += 2 * d[dist];
            }
 
            // Update the current count of the
            // distance
            d[dist]++;
        }
    }
 
    // Return the count of triplets
    return count;
}
 
// Driver Code
int main()
{
 
    // Set of points in plane
    vector > arr = { { 0, 0 },
                                 { 1, 0 },
                                 { 2, 0 } };
 
    // Function call
    cout << countTriplets(arr);
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
class GFG{
 
    // Function to count the triplets
    static int countTriplets(int p[][])
    {
     
        // Initialise count
        int count = 0;
     
        // Traverse the arr[]
        for (int i = 0; i < p.length; i++) {
     
            // Map to store the distance between
            // every pairs p[i] and p[j]
            HashMap d = new HashMap();
     
            for (int j = 0; j < p.length; j++) {
     
                // Find the distance
                int dist = (int)(Math.pow(p[j][1] - p[i][1], 2)+ Math.pow(p[j][0] - p[i][0], 2));
     
                // If count of distance is greater
                // than 0, then find the count
                 
                if (d.containsKey(dist) && d.get(dist) > 0) {
                    count += 2 * d.get(dist);
                }
     
                // Update the current count of the
                // distance
                if (d.containsKey(dist)){
                    d.put(dist,d.get(dist)+1);
                }
                else
                    d.put(dist,1);
            }
        }
     
        // Return the count of triplets
        return count;
    }
     
    // Driver Code
    public static void main(String args[])
    {
     
        // Set of points in plane
        int arr[][] = { { 0, 0 },
                                    { 1, 0 },
                                    { 2, 0 } };
     
        // Function call
        System.out.println(countTriplets(arr));
         
    }
}
 
// This code is contributed by AbhiThakur


Python3
# Python3 program for the above approach
 
# Function to count the triplets
def countTriplets(p) :
 
    # Initialise count
    count = 0;
 
    # Traverse the arr[]
    for i in range(len(p)) :
 
        # Map to store the distance between
        # every pairs p[i] and p[j]
        d = {};
 
        for j in range(len(p)) :
 
             
            # Find the distance
            dist = pow(p[j][1] - p[i][1], 2) + \
                    pow(p[j][0] - p[i][0], 2);
 
            if dist not in d :
                d[dist] = 0;
                 
            # If count of distance is greater
            # than 0, then find the count
            if (d[dist] > 0) :
                count += 2 * d[dist];
 
            # Update the current count of the
            # distance
            d[dist] += 1;
     
    # Return the count of triplets
    return count;
 
# Driver Code
if __name__ == "__main__" :
 
    # Set of points in plane
    arr = [ [ 0, 0 ],
            [ 1, 0 ],
            [ 2, 0 ] ];
 
    # Function call
    print(countTriplets(arr));
     
# This code is contributed by Yash_R


C#
// C# program for the above approach 
using System;
using System.Collections.Generic;
 
class GFG{
     
// Function to count the triplets
static int countTriplets(int[,] p)
{
     
    // Initialise count
    int count = 0;
   
    // Traverse the arr[]
    for(int i = 0; i < p.GetLength(0); i++)
    {
         
        // Map to store the distance between
        // every pairs p[i] and p[j]
        Dictionary d = new Dictionary();
   
        for(int j = 0; j < p.GetLength(0); j++)
        {
             
            // Find the distance
            int dist = (int)(Math.Pow(p[j, 1] -
                                      p[i, 1], 2) +
                             Math.Pow(p[j, 0] -
                                      p[i, 0], 2));
   
            // If count of distance is greater
            // than 0, then find the count
            if (d.ContainsKey(dist) && d[dist] > 0)
            {
                count += 2 * d[dist];
            }
   
            // Update the current count of the
            // distance
            if (d.ContainsKey(dist))
            {
                d[dist]++;
            }
            else
                d.Add(dist, 1);
        }
    }
   
    // Return the count of triplets
    return count;
}
 
// Driver code
static void Main()
{
     
    // Set of points in plane
    int[,] arr = new int [3, 2]{ { 0, 0 },
                                 { 1, 0 },
                                 { 2, 0 } };
   
    // Function call
    Console.WriteLine(countTriplets(arr));
}
}
 
// This code is contributed by divyeshrabadiya07


Javascript


输出:
2

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