📜  按升序打印二维坐标点,然后是它们的频率

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

给定两个数组x[]y[] ,其中x[i]表示x坐标, y[i]表示二维点对应的 y 坐标,任务是按升序打印坐标点,然后是它们的频率。
例子:

方法:这个想法是使用具有键作为对(x[i], y[i])和映射值作为同一点的整数频率的映射。键值将存储坐标对,映射值将存储它们各自的频率。
下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Function to print the coordinates along with
// their frequency in ascending order
void Print(int x[], int y[], int n)
{
 
    // map to store the pairs
    // and their frequencies
    map, int> m;
 
    // Store the coordinates along
    // with their frequencies
    for (int i = 0; i < n; i++)
        m[make_pair(x[i], y[i])]++;
 
    map, int>::iterator i;
 
    for (i = m.begin(); i != m.end(); i++) {
        cout << (i->first).first << " "
             << (i->first).second << " "
             << i->second << "\n";
    }
}
 
// Driver code
int main()
{
    int x[] = { 1, 2, 1, 1, 1 };
    int y[] = { 1, 1, 3, 1, 3 };
    int n = sizeof(x) / sizeof(int);
 
    Print(x, y, n);
 
    return 0;
}


Python3
# Python3 implementation of the approach
 
# Function to print the coordinates along with
# their frequency in ascending order
def Print(x, y, n):
 
    # map to store the pairs
    # and their frequencies
    m = dict()
 
    # Store the coordinates along
    # with their frequencies
    for i in range(n):
        m[(x[i], y[i])] = m.get((x[i],
                                 y[i]), 0) + 1
 
    e = sorted(m)
     
    for i in e:
        print(i[0], i[1], m[i])
 
# Driver code
x = [1, 2, 1, 1, 1]
y = [1, 1, 3, 1, 3]
n = len(x)
 
Print(x, y, n)
 
# This code is contributed
# by mohit kumar


C#
// C# implementation of
// the above approach
using System;
using System.Collections.Generic;
class GFG{
 
public class store :
       IComparer>
{
  public int Compare(KeyValuePair x,
                     KeyValuePair y)
  {
    if(x.Key != y.Key)
    {
      return x.Key.CompareTo(y.Key);   
    }
    else
    {
      return x.Value.CompareTo(y.Value);   
    }
 
  }
}
 
// Function to print the
// coordinates along with
// their frequency in
// ascending order
static void Print(int []x,
                  int []y, int n)
{
  // Map to store the pairs
  // and their frequencies
  SortedDictionary, int> m =
        new SortedDictionary,
                                          int>(new store());
 
  // Store the coordinates along
  // with their frequencies
  for (int i = 0; i < n; i++)
  {
    KeyValuePair tmp =
       new KeyValuePair(x[i], y[i]);
     
    if(m.ContainsKey(tmp))
    {
      m[tmp]++;
    }
    else{
      m[tmp] = 1;
    }
  }
 
  foreach(KeyValuePair, int> i in m)
  {
    Console.Write(i.Key.Key + " " +
                  i.Key.Value + " " +
                  i.Value + "\n");
  }
}
 
// Driver code
public static void Main(string[] args)
{
  int []x = {1, 2, 1, 1, 1};
  int []y = {1, 1, 3, 1, 3};
  int n = x.Length;
  Print(x, y, n);
}
}
 
// This code is contributed by rutvik_56


输出:
1 1 2
1 3 2
2 1 1

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

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