Write a method to return the minimum number of coins needed to match a given value. Assume coin denominations are 25, 10, 5, 1. Example: If the value is 101, then we require four 25 cents coins and one 1 cent coin, so total is 5 coins. public static int coinCount( int value ){ int [] denominations = {25, 10, 5, 1}; int coinCount = 0; for ( int i = 0; i < denominations . length ; i ++){ if ( value % denominations [ i ] == 0){ coinCount = coinCount + ( value / denominations [ i ]); break ; } if ( value % denominations [ i ] > 0){ int r = value / denominations [ i ]; coinCount = coinCount + r ; value = value % denominations [ i ]; } } return coinCount ; }
Comments
Post a Comment