Connection Status:
Competition Arena > AverageCandyLifetime
SRM 392 · 2008-03-06 · by darnley · Simple Math
Class Name: AverageCandyLifetime
Return Type: double
Method Name: getAverage
Arg Types: (vector<int>)
Problem Statement

Problem Statement

On January 1, 2007, a confectioner made several candies.

On the last day of each month she allows her children to eat several of those candies.

The lifetime of a candy is the number of days between January 1 and the day the candy is eaten, inclusive. For example, the lifetime of a candy eaten on January 31 is 31, and the lifetime of a candy eaten on December 31 is 365 (note that 2007 wasn't a leap year).

You are given a int[] eatenCandies, the i-th element of which is the number of candies eaten on the last day of the i-th month of 2007 (January is month 0, February is month 1, etc.). Return the average lifetime of the candies.

Notes

  • The year 2007 was not a leap year.
  • The number of days in the months of 2007, in order, were 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30 and 31.
  • The returned value must be accurate to within a relative or absolute value of 1E-9.

Constraints

  • eatenCandies will contain exactly 12 elements.
  • Each element of eatenCandies will be between 0 and 1000, inclusive.
  • The sum of all the elements in eatenCandies will be greater than 0.
Examples
0)
{1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0}
Returns: 60.5

One candy was eaten on January 31 and the other was eaten on March 31. The lifetimes of the candies are 31 and 31+28+31=90. The average lifetime is (31+90)/2=60.5.

1)
{0, 1000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
Returns: 59.0

All candies were eaten on February 28. The lifetime of each candy is 31+28=59, so the average candy lifetime is 59.0.

2)
{0, 0, 0, 0, 0, 1, 0, 0, 0, 50, 0, 0}
Returns: 301.5882352941176

Most of the candies were eaten on October 31 (Halloween), and the lifetime of each of those candies is 304. The average lifetime is smaller than 304, because of a candy with lifetime 181, eaten on June 30.

3)
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1000}
Returns: 365.0

Eating all candies on the New Year's Eve makes the maximum possible average lifetime.

4)
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}
Returns: 252.80769230769232

Submissions are judged against all 40 archived test cases, of which 5 are shown here. Case numbers match the judge’s.

Coding Area

Language: C++17 · define a public class AverageCandyLifetime with a public method double getAverage(vector<int> eatenCandies) · 40 test cases · 2 s / 256 MB per case

Submitting as anonymous