Instantly share code, notes, and snippets.

# marsbomber/gist:4325345 Last active Dec 9, 2015

Rework on uni year one assignment question. Given any amount in cents, calculate changes
 module ChangeMachine class IdiotEncountered < StandardError; end AVAILABLE_UNITS = [10000, 5000, 2000, 1000, 500, 200, 100, 50, 20, 10, 5] def self.show_me_the_money(dollar_in_cents) raise IdiotEncountered, "You are an idiot, no such amount in AUS..." unless dollar_in_cents % 5 == 0 dispense(dollar_in_cents) end def self.dispense(dollar_in_cents) return {} if dollar_in_cents == 0 unit = AVAILABLE_UNITS.select{|u| u <= dollar_in_cents}.max {unit => (dollar_in_cents / unit)}.merge(dispense(dollar_in_cents % unit)) end end puts ChangeMachine.show_me_the_money(37500).inspect
Owner Author

### marsbomber commented Dec 18, 2012

 Original version written in 2001 :) My first ever program I think ```//Name: Jim Li //Subject: CS108 //Program: Assignment 1 //File name: Change.java //Date: 19/03/2001 //Program description: This is a program that will give the use a smallest number of change of coins. import java.io.*; public class Change { public static void main (String[] args) throws IOException { BufferedReader stdin=new BufferedReader (new InputStreamReader(System.in)); String x; System.out.println("Input the amount of change in cents"); System.out.println("between 0 cents and 499 cents"); x=stdin.readLine(); int y = Integer.parseInt(x); if(y>=0&&y<500) { int a = (int)(y/200); int b = (int)((y-200*a)/100); int c = (int)((y-200*a-100*b)/50); int d = (int)((y-200*a-100*b-50*c)/20); int e = (int)((y-200*a-100*b-50*c-20*d)/10); int f = (int)((y-200*a-100*b-50*c-20*d-10*e)/5); int g = (int)((y-200*a-100*b-50*c-20*d-10*e-5*f)/1); int h = a+b+c+d+e+f+g; System.out.print("\$2 coins "); System.out.println(a); System.out.print("\$1 coins "); System.out.println(b); System.out.print("50 cents coins "); System.out.println(c); System.out.print("20 cents coins "); System.out.println(d); System.out.print("10 cents coins "); System.out.println(e); System.out.print("5 cents coins "); System.out.println(f); System.out.print("1 cents coins "); System.out.println(g); System.out.print("Total Number of Coins "); System.out.println(h); } else System.out.println("input error"); } }```

### tommyli commented Dec 18, 2012

 I like your original version better :P
Owner Author

### marsbomber commented Dec 18, 2012

 @tommyli that's the exact version I submitted ... Now thinking about it, tutoring is hard work. It's like a torture EVERY single time you mark a year one student assignment... well most of them anyway