double findMaxAverage(vector<int>& nums, int k) {
    double max = INT_MIN;
    int len = nums.size();

    if (len <= 4)
    {
        int sum = 0;
        for (int i = 0; i < len; i++)
        {
            sum += nums[i];
        }
        max = sum;
    }
    else
    {
        for (int i = 0; i <= len - k; i++)
        {
            int sum = 0;
            for (int j = i; j < i + k; j++)
            {
                sum += nums[j];
            }

            if (max < sum)
            {
                max = sum;
            }
        }
    }
    
    double avg = max / k;
    return avg;
}

 

posted on 2018-09-26 17:00  Sempron2800+  阅读(97)  评论(0编辑  收藏  举报