Solução: Eleições - SPOJ (Diogo Maron)

From AdonaiMedrado.Pro.Br
Jump to: navigation, search
#include <stdio.h>
 
int compara(x, y)
void *x, *y; /* Declaração antiga do ANSI C, mas muito útil */
{
    if ( *(int*)x > *(int*)y )
       return 1;
    else if ( *(int*)x == *(int*)y )
            return 0;
    else if ( *(int*)x < *(int*)y )
            return -1;
}
 
int main()
{
	int n, i, num;
	int votosVencedor, vencedor, auxVencedor, auxVotos;
	int *votos;
	scanf("%d",&n);
	votos = (int *)malloc(n * sizeof(int));
	for (i = 0; i < n; i++)
	{
		scanf("%d", &num);
		votos[i] = num;
	}
	qsort( votos, (size_t) n, sizeof(int), compara );
	votosVencedor = 0;
	vencedor = votos[0];
	auxVencedor = votos[0];
	auxVotos = 1;
	for (i = 1; i < n; i++)
	{
		if (votos[i] == auxVencedor)
			auxVotos++;
		else 
		{
			if (auxVotos > votosVencedor)
			{
				votosVencedor = auxVotos;
				vencedor = auxVencedor;
			}
			auxVencedor = votos[i];
			auxVotos = 1;
		}
	}
	if (auxVotos > votosVencedor)
	{
		votosVencedor = auxVotos;
		vencedor = auxVencedor;
	}
	printf("%d\n", vencedor);
	return 0;	
}