TopCoder problem "AverageCandyLifetime" used in SRM 392 (Division II Level One)



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.

 

Definition

    
Class:AverageCandyLifetime
Method:getAverage
Parameters:int[]
Returns:double
Method signature:double getAverage(int[] eatenCandies)
(be sure your method is public)
    
 

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)
    
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}
Returns: 252.80769230769232

Problem url:

http://www.topcoder.com/stat?c=problem_statement&pm=8707

Problem stats url:

http://www.topcoder.com/tc?module=ProblemDetail&rd=11126&pm=8707

Writer:

darnley

Testers:

PabloGilberto , Olexiy , ivan_metelsky , ged

Problem categories:

Simple Math