#182. 数组两半平均值比较
数组两半平均值比较
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 .
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
scoresarray, 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 .
The second half is [4, 4], its average is .
The larger average is .
Sample 2:
The array is [4, 4, 4, 2, 2, 2].
The first half is [4, 4, 4], its average is .
The second half is [2, 2, 2], its average is .
The larger average is .
Sample 3:
The array is [3, 4, 5, 1, 2, 3].
The first half is [3, 4, 5], its average is .
The second half is [1, 2, 3], its average is .
The larger average is .
Constraints
For each test case, the time limit is second, and the memory limit is KiB.
The length of the scores array will be at least .
The values of array elements are within a reasonable integer range.