G : Snowbunnies

07Dec13

I recently participated in the Advent Programming Contest 2013 organised by the IEEE Student Branch Klagenfurt (with support from Alpen-Adria-Universität Klagenfurt), wherein – as I’d recently received from Udacity a certificate for Introduction to Programming : Problem Solving with Java (CS046) – I challenged myself to program my solutions to the problems posed by the contest using only Java (I’d used C and C++ when I’d participated in the contest in 2012); I solved eighteen of the problems posed by the contest, achieving a rank of 10th (out of 155 participants).

As submissions of solutions to the contest are now closed, I’m posting here the solutions I submitted to the contest.

Problem (G; medium difficulty):

Snowbunnies

In a snowy field near Santa’s home, there is a population of white bunnies, all adults. Adult bunnies create offspring (one per adult) every 15 days, but 10% of the newborn bunnies die shortly after the were born. They mature after 30 days, but 30% of the matured bunnies leave the forest. Every 15 days the bunny population catches a flu, which kills 25% of the overall population (flu strikes after new bunnies are born).

Problem

Write a program that calculate the number of bunnies alive in the forest after 1 year. The amount of bunnies should always be rounded down to the next integer. The program shall read the initial number of bunnies from stdin. After writing out the result, the program shall terminate.

Example 1

Input

10

Output

0

Example 2

Input

1000

Output

1786

Solution (G.java):

import java.util.Scanner;

public class G
{
  public static void main( String p[] )
  {
    Scanner s = new Scanner( System.in );
    double adult_bunnies = s.nextInt(), fifteen_day_old_bunnies = 0, newborn_bunnies = 0;
    for( int i = 15; i < 365; i += 15 )
    {
      double thirty_day_old_bunnies = fifteen_day_old_bunnies;

      fifteen_day_old_bunnies = newborn_bunnies;

      newborn_bunnies = adult_bunnies * 0.9;
      newborn_bunnies = ( int )newborn_bunnies;

      thirty_day_old_bunnies *= 0.7;
      adult_bunnies += ( int )thirty_day_old_bunnies;

      newborn_bunnies *= 0.75;
      newborn_bunnies = ( int )newborn_bunnies;

      fifteen_day_old_bunnies *= 0.75;
      fifteen_day_old_bunnies = ( int )fifteen_day_old_bunnies;

      adult_bunnies *= 0.75;
      adult_bunnies = ( int )adult_bunnies;
    }
    System.out.println(( int )( adult_bunnies + fifteen_day_old_bunnies + newborn_bunnies ));
  }
}

Testing:

10
0
1000
1786
Advertisements


No Responses Yet to “G : Snowbunnies”

  1. Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: