Pagini recente » Cod sursa (job #200073) | Cod sursa (job #1258257) | Cod sursa (job #1579751) | Cod sursa (job #441138) | Cod sursa (job #2084979)
#include <iostream>
#include<algorithm>
#include <fstream>
using namespace std;
ifstream f("elmaj.in");
ofstream g("elmaj.out");
int main()
{
int a[10],i,n,count=0;int majorityElement;
f >> n;
for(int i = 0; i < n; i++)f >> a[i];
for (i = 0 ; i < n ; i++)
{
if (count == 0) {
majorityElement = a[i];
count = 1;
}
else
{
if(a[i] == majorityElement)
count++;
else
count--;
}
}
count = 0;
for (i = 0; i < n; i++) {
if (a[i] == majorityElement) {
count++;
}
}
if (count > (n/2)) {
g<< majorityElement<<" "<<count;
}
else g<<"-1" ;
return 0;
}