Pagini recente » Cod sursa (job #2982210) | Cod sursa (job #920322) | Cod sursa (job #3200127) | Cod sursa (job #1110147) | Cod sursa (job #635179)
Cod sursa(job #635179)
#include<fstream>
#include<hash_map>
using namespace std;
const char inFile[] = "elmaj.in";
const char outFile[] = "elmaj.out";
hash_map<int, int> hashTable;
int N;
int element = -1;
void Citire();
void Afisare();
void Find(int value);
void Find(int value)
{
hash_map<int, int>::iterator itr = hashTable.find(value);
if(itr == hashTable.end())
{
hashTable.insert(make_pair(value, 1));
}
else
{
itr->second++;
if(element == -1 && itr->second > N/2)
{
element = itr->first;
}
}
}
void Citire()
{
fstream fin(inFile, ios::in);
fin >> N;
int current;
for(int i = 0 ; i < N; i++)
{
fin >> current;
Find(current);
}
fin.close();
}
void Afisare()
{
fstream fout(outFile, ios::out);
if( element == -1)
{
fout << "-1\n";
}
else
{
fout << element << " " << hashTable.find(element)->second << "\n";
}
fout.close();
}
int main(int argc, char* argv[])
{
Citire();
Afisare();
}