Pagini recente » Cod sursa (job #856797) | Cod sursa (job #769315)
Cod sursa(job #769315)
#include<stdio.h>
#include<stdlib.h>
const unsigned int Multiplier = 0x6b43a9b5; /* 2^30 < M < 2^31, M prime*/
struct list{
unsigned int value;
unsigned int nr;
struct list *next;
} *hash[1048576];/* 1048576 = 2^20 */
FILE *file;
unsigned int maxNr, maxVal, N;
int main()
{
unsigned int hashVal, x;
struct list *p;
file = fopen("elmaj.in", "r");
fscanf(file, "%d", &N);
while(EOF != fscanf(file, "%d", &x) && (maxNr < (N>>1)+1))
{
hashVal = (x * Multiplier) >> 12;
for(p = hash[hashVal]; p && p->value != x; p = p->next);
if(p)
{
p->nr++;
if(p->nr > maxNr)
maxNr = p->nr,
maxVal = p->value;
}
else
{
p = malloc(sizeof(struct list));
p->value = x;
p->nr = 1;
p->next = hash[hashVal];
hash[hashVal] = p;
}
}
fclose(file);
file = fopen("elmaj.out", "w");
if(maxNr > (N>>1))
fprintf(file, "%d %d\n", maxVal, maxNr);
else
fprintf(file, "-1\n");
fclose(file);
return 0;
}