/**
* Coin Change Problem
* @author r.prateek
*
*/
class CoinChange {
static int[] coins = { 30 ,24 , 12 , 6 , 3 , 1 };
static int W = 53 ;
/*static int[] coins = { 1 , 2 , 5 };
static int W = 7 ;*/
public static void main
(String[] args
) {
CoinChange obj=new CoinChange();
obj.coinChange(coins, 1);
System.
out.
println("Minimum Coins Required : " + fValues
[W
]); System.
out.
println("Coin denomindations required are :"); obj.print(W);
}
static int index=0;
static int [] S = new int[W + 1];
static int[] fValues= new int[W+1] ;
public void coinChange(int[] coins , int w){
if(w > W)
return ;
int min=w , fValue , coin = 0;
for(int i=0;i<coins.length ;i++) {
if(w >= coins[i]) {
fValue = fValues[w-coins[i]] ;
if(min > fValue)
{
min = fValue;
coin=i;
}
}
}
fValues[++index] = min +1 ;
S[w] = coin ;
coinChange(coins , w + 1);
}
public void print(int W ){
while(W>0) {
System.
out.
print(coins
[S
[W
]] + "\t"); W=W-coins[S[W]];
}
}
}
LyoqCiAqIENvaW4gQ2hhbmdlIFByb2JsZW0KICogQGF1dGhvciByLnByYXRlZWsKICoKICovCiBjbGFzcyBDb2luQ2hhbmdlIHsKCXN0YXRpYyBpbnRbXSBjb2lucyA9IHsgMzAgLDI0ICwgMTIgLCA2ICwgMyAsIDEgfTsKCXN0YXRpYyBpbnQgVyA9IDUzIDsKCgkvKnN0YXRpYyBpbnRbXSBjb2lucyA9IHsgMSAsIDIgLCA1IH07CglzdGF0aWMgaW50IFcgPSA3IDsqLwoJCiAgICAKCXB1YmxpYyBzdGF0aWMgdm9pZCBtYWluKFN0cmluZ1tdIGFyZ3MpIHsKCgkJQ29pbkNoYW5nZSBvYmo9bmV3IENvaW5DaGFuZ2UoKTsKCQlvYmouY29pbkNoYW5nZShjb2lucywgMSk7CgkJU3lzdGVtLm91dC5wcmludGxuKCJNaW5pbXVtIENvaW5zIFJlcXVpcmVkIDogIiArIGZWYWx1ZXNbV10pOwoJCVN5c3RlbS5vdXQucHJpbnRsbigiQ29pbiBkZW5vbWluZGF0aW9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