B : Weekdays in December

02Dec13

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 (B; easy difficulty):

Weekdays in December

Sometimes, advent can be a terribly stressy time. One has to finish things that need to be done this year, get presents for you loved ones and organize christmas dinner. Eventually, one does not know anymore what weekday it is.

Problem

Implement a program that tells you the current weekday based on the date in December for this year. The program should read line by line from standard input. If a line contains an integer number between 1 and 31, calculate the respective weekday and print it (English language, capitalized first letter) to standard output. If the input is 0, the program should terminate.

Sample Input

1
16
7
0

Sample Output

Sunday
Monday
Saturday

Solution (B.java):

import java.util.Scanner;

public class B
{
  public static void main( String p[] )
  {
    Scanner s = new Scanner( System.in  );
    int i = s.nextInt();
    while( i != 0 )
    {
      byte day_of_week = 7;
      for( int k = 1; k < i; k ++ )
        if( ++ day_of_week > 7 )
          day_of_week = 1;
      System.out.println( new String[]{ "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" }[ day_of_week - 1 ] );
      i = s.nextInt();
    }
  }
}

Testing:

1
Sunday
16
Monday
7
Saturday
0
Advertisements


No Responses Yet to “B : Weekdays in December”

  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: