P : Tax calculation

16Dec13

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

Tax calculation

As everybody knows, Santa and the elves live at the north pole. The north pole has been recently claimed by several countries, among them Canada, Denmark, Russia, and USA. Living on a national territory, the elves now face the problem that they have to pay tax for their income.

Luckily, due to the concurrent claims of the countries, they can chose, which tax model they want to have applied.

The tax models, a specific north pole edition, are as follows (for simplicity, all currencies are in Euro):

  • Canada: 26% flat tax
  • Denmark: first 1000 Euro no tax, 10% on the second 1000 (in addition to it), 20% on the third 1000 (in addition to it), and so on…
  • Russia: first 3000 Euro 10% tax, everything in addition to it 40%
  • U.S: the overall tax is 12%, if the income is below 1500 Euro, 25% if the income is between 1500.01 and 6000 Euro, 38% if the income is between 6000.01 and 10000 Euro, and 15% if the income is above 10000.01 Euro.

Problem

Write a program for Santa and the elves that recommends the best tax model for a given income. The first line of input consists of the number of income values. For each income, determine the country where one would pay the lowest tax for this income and print out the name of the respective country. If more than one country offer the lowest tax, print all of those countries alphabetically seperated by a comma and space.

Example

Input

4
1000
6100
10000
15000

Output

Denmark
Russia
Canada
USA

Solution (P.java):

import java.util.Scanner;

public class P
{
  private static final String COUNTRY_NAME[] = { "Canada", "Denmark", "Russia", "USA" };

  public static void main( String p[] )
  {
    Scanner s = new Scanner( System.in );
    final int K = s.nextInt();
    for( int i = 0; i < K; i ++ )
    {
      final double income = s.nextDouble();
      final double tax_paid[] = { Canada( income ), Denmark( income ), Russia( income ), USA( income ) };
      double least_tax_paid = tax_paid[ 0 ];
      for( int m = 1; m < tax_paid.length; m ++ )
        if( tax_paid[ m ] < least_tax_paid )
          least_tax_paid = tax_paid[ m ];
      boolean b = false;
      for( int m = 0; m < tax_paid.length; m ++ )
        if( tax_paid[ m ] == least_tax_paid )
        {
          if( b )
            System.out.print( ", " );
          b = true;
          System.out.print( COUNTRY_NAME[ m ] );
        }
      System.out.println();
    }
  }

  private static double Canada( double p )
  {
    return p * 0.26;
  }

  private static double Denmark( double p )
  {
    p -= 1000;
    double tax_rate = 0, tax_paid = 0;
    while( p > 0 )
    {
      tax_rate += 10;
      tax_paid += ((( p < 1000 )?p:1000 ) * ( tax_rate / 100 ));
      p -= 1000;
    }
    return tax_paid;
  }

  private static double Russia( double p )
  {
    double tax_paid = (( p < 3000 )?p:3000 ) * 0.10;
    p -= 3000;
    if( p > 0 )
      tax_paid += ( p * 0.40 );
    return tax_paid;
  }

  private static double USA( double p )
  {
    double tax_rate = 15;
    if( p <= 10000 )
      tax_rate = 38;
    if( p <= 6000 )
      tax_rate = 25;
    if( p <= 1500 )
      tax_rate = 12;
    return p * ( tax_rate / 100 );
  }
}

Testing:

4
1000
Denmark
6100
Russia
10000
Canada
15000
USA
Advertisements


No Responses Yet to “P : Tax calculation”

  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: