Coin Determiner Coderbyte Solution

0

Using the Java language, have the function CoinDeterminer(num) take the input, which will be an integer ranging from 1 to 250, and return an integer output that will specify the least number of coins, that when added, equal the input integer. Coins are based on a system as follows: there are coins representing the integers 1, 5, 7, 9, and 11. 

for example: if num is 16, then the output should be 2 because you can achieve the number 16 with the coins 9 and 7. If num is 25, then the output should be 3 because you can achieve 25 with either 11, 9, and 5 coins or with 9, 9, and 7 coins.

Coin Determiner Coderbyte Solution In Java


import java.util.Scanner;


public class CoinDeterminer {
final int[] coins = new int[] {1, 5, 7, 9, 11};

int CoinDeterminer(int num) {
int minCount = Integer.MAX_VALUE;
for (int i = coins.length - 1; i >= 0; i--) {
for (int j = i; j >= 0; j--) {
int count = getCoinCount(num, i, j);
if (count < minCount) {
minCount = count;
}
}
}

return minCount;

}

int getCoinCount(int num, int maxCoinIndex,
int startCoinIndex) {
int count = 0;

if (num >= coins[maxCoinIndex]) {
num -= coins[maxCoinIndex];
count++;
}

for (int i = startCoinIndex; i >= 0; i--) {
while (num >= coins[i]) {
num -= coins[i];
count++;
}
}
return count;
}

public static void main (String[] args) {
// keep this function call here
Scanner s = new Scanner(System.in);
CoinDeterminer c = new CoinDeterminer();
System.out.print(c.CoinDeterminer(34));
}
}


Coin Determiner Coderbyte Solution In Python


import sys
def CoinDeterminer(num):
coins = [11, 9, 7, 5, 1]
count = sys.maxint
for i in range(len(coins)):
tCount = 0
tNum = num
for j in range(i, len(coins)):
tCount += tNum / coins[j]
tNum = tNum % coins[j]
if 0 < tCount < count:
count = tCount
return count
# keep this function call here
# to see how to enter arguments in Python
scroll down
print CoinDeterminer(raw_input())


Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.
Post a Comment (0)
Our website uses cookies to enhance your experience. Learn More
Accept !