微信红包

代码如下:

#include 
#include 
#include 
#include 
#include 
using namespace std;

int GetNumOfIndex(vector & arr, int p, int r, int nIndex) 
{
    if (p >= r) return -1;

    srand((unsigned int)time(NULL));    
    swap(arr[rand() % (r-p) + p], arr[r-1]);

    int k = p;
    for (int i=p; i & arr)
{
    if (arr.empty()) return 0;

    int l = arr.size();  // arr size

    int ln1 = (l-1) / 2;
    int ln2 = ln1 + (l-1) % 2;

    vector arrTemp(arr.begin(), arr.end());

    int nNum1 = GetNumOfIndex(arrTemp, 0, arrTemp.size(), ln1);
    int nNum2 = GetNumOfIndex(arrTemp, ln1, arrTemp.size(), ln2);

    int nNum1Count = 0;
    int nNum2Count = 0;
    for (int i=0; i= l) return nNum1;
    else if (nNum2Count * 2>= l) return nNum2;
    else return 0;
}

int Calculate2(const vector & arr)
{
    if (arr.empty()) return 0;

    int nNum = arr[0];
    int nCount = 1;
    int l = arr.size();  // arr size
    for (int i=1; i= l ? nNum : 0;
}

int main(int argc, char ** argv) 
{
    vector arr = {1, 2, 3, 2, 2, 5, 5, 2};

    time_t t1 = time(NULL);
    for (int i=0; i<1000000; ++i) {
        Calculate(arr);
    }
    time_t t2 = time(NULL);
    cout << t2 - t1 << endl;

    t1 = time(NULL);
    for (int i=0; i<1000000; ++i) {
        Calculate2(arr);
    }
    t2 = time(NULL);
    cout << t2 - t1 << endl;

    return 0;
}

你可能感兴趣的:(微信红包)