Coding Exercise – Timus Online Judge – 1880. Psych Up’s Eigenvalues – C++ solution


1880 Coding Exercise - Timus Online Judge - 1880. Psych Up's Eigenvalues - C++ solution algorithms brute force c / c++ code implementation programming languages timus

The puzzle is from Timus Online Judge.

Despite detailed description, which may be confusing, the puzzle is really simple to solve. It asks you to find out the identical numbers that appear in all (three) lists.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <map>
using namespace std;
// codingforspeed.com
 
int main()
{
    map<int, int> arr;
    int ans = 0;
    for (int i = 0; i < 3; i ++)    
    {       
        int n;      
        cin >> n;
        for (int j = 0; j < n; j ++)        
        { 
            int v;          
            cin >> v;
            if (++arr[v] == 3)
            {
                ans ++;
            }           
        }
    }
    cout << ans;
    return (0);
}
#include <iostream>
#include <map>
using namespace std;
// codingforspeed.com

int main()
{
	map<int, int> arr;
	int ans = 0;
	for (int i = 0; i < 3; i ++) 	
	{ 		
		int n; 		
		cin >> n;
		for (int j = 0; j < n; j ++) 		
		{ 
			int v; 			
			cin >> v;
			if (++arr[v] == 3)
			{
				ans ++;
			}			
		}
	}
	cout << ans;
	return (0);
}

We use a std::map data structure to hold the counter for a key. If a key appears three times, then increment the answer. The std::map supports the template (you can define the type of key and value, the keypair on the fly). In this case, both are integers.

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
249 words
Last Post: Reverse List/Tuple/String in Python
Next Post: Coding Exercise - Timus Online Judge - C++ solution - 1197. Lonesome Knight

The Permanent URL is: Coding Exercise – Timus Online Judge – 1880. Psych Up’s Eigenvalues – C++ solution

Leave a Reply