微软面试题:某水王的发帖数超过总贴数的一半,找出之
//某水王的发帖数超过总贴数的一半,找出之
int find(int *ID, int N)
{
int candidate;
int nTimes, i;
for (i = nTimes = 0; i < N; i++)
{
if (nTimes == 0)
{
candidate = ID[i];
nTimes = 1;
}
else if (candidate == ID[i])
{
nTimes++;
}
else
{
nTimes--;
}
}
return candidate;
}