Monday, February 23, 2015

BritishCoins

Problem:

 TopCoder Problem Statement - BritishCoins Single Round Match 165 Round 1 - Division II, Level One Single Round Match 232 Round 1 - Division II, Level One

Overview:

Convert the given number of pence into pounds, shillings, and pennies.

Java Source:
```01: public class BritishCoins {
02:
03:     private static final int SHILLINGS_PER_POUND = 20;
04:     private static final int PENNIES_PER_SHILLING = 12;
05:
06:     private static final int PENNIES_PER_POUND =
07:             PENNIES_PER_SHILLING * SHILLINGS_PER_POUND;
08:
09:     public int[] coins(int pence)  {
10:
11:         int[] ret = new int[3];
12:
13:         ret[0] = pence / PENNIES_PER_POUND;
14:         ret[1] = (pence % PENNIES_PER_POUND) / PENNIES_PER_SHILLING;
15:         ret[2] = pence % PENNIES_PER_SHILLING;
16:
17:         return ret;
18:     }
19: }
```
Notes:

Start by declaring constants to hold the number of shillings in a pound and the number of pennies in a shillings. Using that, we can calculate the number of pennies in a shilling (20 * 12 = 240). From here we can use simple math to derive our answer.

Remember that in Java, integer division chops off the remainder. so 10 / 3 = 3. Therefore, the number of pounds, is simply the number of pence / PENNIES_PER_POUND.

The moduls operator % gives us the remainder. To calculate the number of shillings, we first get the remainder of after taking out as many pounds as possible (pence % PENNIES_PER_POUND). Then, just as before, we divide that by PENNIES_PER_SHILLING to get the number of shillings.

The number of pennies remaining is just pence % PENNIES_PER_SHILLING.

Just store each of those values in the correct location of an int[] and the problem is solved.