#182. 数组两半平均值比较

    ID: 182 Type: Default 1000ms 256MiB Tried: 0 Accepted: 0 Difficulty: (None) Uploaded By: Tags>codingbatAp-1gesp1循环结构数学基础

数组两半平均值比较

Compare Averages of Array Halves

Background

Congcong is learning how to process array data. He encountered a task that requires comparing the averages of two halves of an array.

Problem Description

Given an integer array of scores, scores, compute the integer average of its first half and its second half, and return whichever is larger. We define the second half to begin at index length/2. The array length will be at least 22.

To practice decomposition, write a separate helper method int average(int[] scores, int start, int end) which computes the integer average of the elements in the scores array between indexes start and end (inclusive). Then, call this helper method twice in your main method scoresAverage() to implement the functionality.

Normally, averages would be computed with doubles, but here we use integers, so the expected results are exact.

Input Format

Input is given from standard input in the following format.

A single line containing multiple integers, representing the elements of the scores array, separated by spaces.

Output Format

Output is printed to standard output in the following format.

An integer, representing the larger of the two half-averages.

Sample

2 2 4 4
4
4 4 4 2 2 2
4
3 4 5 1 2 3
4

Sample Explanation

Sample 1: The array is [2, 2, 4, 4]. The first half is [2, 2], its average is (2+2)/2=2(2+2)/2 = 2. The second half is [4, 4], its average is (4+4)/2=4(4+4)/2 = 4. The larger average is 44.

Sample 2: The array is [4, 4, 4, 2, 2, 2]. The first half is [4, 4, 4], its average is (4+4+4)/3=4(4+4+4)/3 = 4. The second half is [2, 2, 2], its average is (2+2+2)/3=2(2+2+2)/3 = 2. The larger average is 44.

Sample 3: The array is [3, 4, 5, 1, 2, 3]. The first half is [3, 4, 5], its average is (3+4+5)/3=4(3+4+5)/3 = 4. The second half is [1, 2, 3], its average is (1+2+3)/3=2(1+2+3)/3 = 2. The larger average is 44.

Constraints

For each test case, the time limit is 11 second, and the memory limit is 10241024 KiB. The length of the scores array will be at least 22. The values of array elements are within a reasonable integer range.