Norway


#include<iostream>

#include<unordered_set>

using namespace std;

 

// structure to store a pair (or use std::pair)

struct Pair {

    int x, y;

};

 

// Function to all that are mirror of each other

// Here we’re creating a template which deduces the size

// from its declared type

template<typename T, size_t n>

void findPairs(T const(& pair)[n])

{

    // create an empty set of strings

    unordered_set<string> set;

 

    // do for each pair

    for (int i = 0; i < n; i++)

    {

        // construct a pair (x, y) from pair[i]

        string p = “{“ + to_string(pair[i].x) + “, “ +

                        to_string(pair[i].y) + “}”;

 

        // insert current pair into the set

        set.insert(p);

 

        // construct mirror pair (y, x) of pair[i]

        string reverse_pair = “{“ + to_string(pair[i].y) + “, “ +

                                    to_string(pair[i].x) + “}”;

 

        // if mirror pair if seen before, print the pairs

        if (set.find(reverse_pair) != set.end())

            cout << p << ” | “ << reverse_pair << ‘n’;

    }

}

 

// main function

int main()

{

    Pair pair[] = { {3, 4}, {1, 2}, {5, 2}, {7, }, {4, 3}, {2, 5} };

    findPairs(pair);

 

    return 0;

}



Source link

LEAVE A REPLY

Please enter your comment!
Please enter your name here