寻找发帖水王问题

寻找“发帖”水王


在一个数组中,有n个数,在数组中出现的次数"大于" "1/(n+1)"


int findN(int* pIn, int iInLen, int n, int* pOut)
{
    memset(pOut, 0, n * sizeof(int));
    
    int* pOutCnt = (int*)malloc(n * sizeof(int));
    if (NULL == pOutCnt) {
        return 0;
    }
    memset(pOutCnt, 0, n * sizeof(int));

    for (int i = 0, j = 0; i < iInLen; ++i) {
        while (j < n) {
            if (0 == pOutCnt[j] || pOut[j] == pIn[i]) {
                pOut[j] = pIn[i];
                pOutCnt[j]++;
                break;
            }
            j++;
        }

        if (j >= n) {
            j = 0;
            while (j < n) {
                pOutCnt[j]--;
                j++;
            }
        }

        j = 0;
    }	

    free(pOutCnt);

    return n;
}

posted @ 2015-08-03 17:10  WONDERFUL_cnblogs  阅读(141)  评论(0编辑  收藏  举报