Labels

algorithms (22) Design Patterns (20) java (19) linux (14) Snippet (13) service mix (6) soa (4)

Change Maker code in Java

/**
*
*/
package org.code.dp;

public class ChangeMaker {

static int makeChange(int amount, int denomination) {

if (denomination == 1) {
System.out.println(
"notes: " + amount + " denom " + denomination);
System.out.println(
"---------------------------------------");
return 1;
}
int next_denom = getNextDenom(denomination);
int ways = 0;
for (int notes = 0; notes * denomination <= amount; notes++) {
System.out.println(
"notes: " + notes + " denom " + denomination);
ways
+= makeChange(amount - notes * denomination, next_denom);
}
return ways;
}

private static int getNextDenom(int denom) {
switch (denom) {
case 5:
return 2;
case 2:
return 1;
case 1:
return 1;
}
return 0;

}
}

No comments:

Post a Comment

Search 24 Bytes