📜  按升序打印二维坐标点,然后打印其频率

📅  最后修改于: 2021-04-24 17:09:51             🧑  作者: 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




想要从精选的最佳视频中学习和练习问题,请查看有关从基础到高级C++的C++基础课程以及有关语言和STL的C++ STL课程。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”