Cod sursa(job #1209548)

Utilizator grayshadeLaurentiu Nicola grayshade Data 17 iulie 2014 23:45:24
Problema Elementul majoritar Scor 80
Compilator cpp Status done
Runda Arhiva educationala Marime 0.64 kb
#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    ifstream f("elmaj.in");

    int votes = 0, candidate, n, k;
    f >> k;
    while (k-- > 0)
    {
        f >> n;
        if (votes == 0)
        {
            candidate = n;
            votes = 1;
        }
        else if (n == candidate)
            votes++;
        else
            votes--;
    }

    f.seekg(ios_base::beg);
    f >> k;
    votes = 0;
    while (k-- > 0)
    {
        f >> n;
        if (n == candidate)
            votes++;
    }

    ofstream of("elmaj.out");
    of << candidate << ' ' << votes << endl;
}