#include "stdafx.h" int find(int * ID, int N) { int cand; int nTimes=0; for(int i=0;i<N;i++) { if(nTimes == 0) { cand = i; nTimes = 1; } else { if(cand == ID[i]) nTimes++; else nTimes--; } } return cand; } //扩展题,有三个发帖很多的ID,发帖数目都超过了1/4 int * find2(int * ID, int N) { int cand[3]; int nTimes[3]={0}; for(int i=0;i<N;i++) { if(nTimes[0] == 0 && cand[1] != ID[i] && cand[2] != ID[i]) { cand[0]=ID[i]; nTimes[0]=1; }else if(nTimes[1] == 0 && cand[0] != ID[i] && cand[2] != ID[i]) { cand[1]=ID[i]; nTimes[1]=1; }else if(nTimes[2] == 0 && cand[0] != ID[i] && cand[1] != ID[i]) { cand[2]=ID[i]; nTimes[2]=1; }else if(cand[0] != ID[i] && cand[1] != ID[i] && cand[2] != ID[i] ){ nTimes[0] -= 1; nTimes[1] -= 1; nTimes[2] -= 1; }else if(cand[0] == ID[i]){ nTimes[0] += 1; }else if(cand[1] == ID[i]){ nTimes[1] += 1; }else if(cand[2] == ID[i]){ nTimes[2] += 1; } } return cand; } int _tmain(int argc, _TCHAR* argv[]) { int ID1[5] = {2,3,1,2,2}; int ID2[10] = {3,3,2,2,1,3,4,4,4}; int n = find(ID1, 5); int * n2 = find2(ID2,10); return 0; }