change2

change2 - / / / / / / Arup Guha Edited 6/13/05 and 9/21/04...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
// Arup Guha // Edited 6/13/05 and 9/21/04 (not known when this was orignally // written.) Added recursive code to solve the change problem. // Re-editied on 6/14/05 to fully generalize the code to work for // any denominations array (in increasing order). / public class change2 { public static int[] denominations = {1, 2, 4, 7}; public static void main(String[] args) { System.out.println("rec: "+makeChangerec(15, 7)); System.out.println("dyn: "+makeChangedyn(15, 7)); } public static int makeChangedyn(int n, int d) { // No way to make change for a negative amount. if (n < 0) return 0; // We can make change for i cents using the smallest coin only // if i is divisible by this coin amount. if (n%denominations[0] == 0) return 1; else return 0; // Build table here. else { int[][] table = new int[denominations.length][n+1]; // We can make change for i cents using the smallest coin only // if i is divisible by this coin amount. for (int i=0; i<n+1;i++) if (i%denominations[0] == 0) table[0][i] = 1; else table[0][i] = 0; // We can make change for 0 cents in 1 way.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 3

change2 - / / / / / / Arup Guha Edited 6/13/05 and 9/21/04...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online