Last active
August 29, 2015 14:13
-
-
Save vasildakov-zz/133c4649e0a9bfc41a65 to your computer and use it in GitHub Desktop.
Hristo Test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
class Test { | |
public static void main(String[] args) | |
{ | |
// the withdraw amount | |
Scanner scb = new Scanner(System.in); | |
System.out.println("Please enter the amount you want to withdraw"); | |
int withdraw = scb.nextInt(); | |
// the banknotes nominals | |
int[] banknotes = {100, 50, 20, 10, 5, 2, 1}; | |
// the basket which holds the banknotes | |
List<Integer> basket = new ArrayList<Integer>(); | |
Map<Integer,Integer> map = new HashMap<Integer,Integer>(); | |
int counter = 0; | |
while( counter < withdraw ) { | |
// System.out.println(counter); | |
for(int i = 0; i < banknotes.length; i++){ | |
// do nothing with nominals higher than the withdraw amount | |
if(banknotes[i] > withdraw) continue; | |
// check the current basket sum | |
if(sum(basket) + banknotes[i] <= withdraw) { | |
// add banknote to the basket | |
basket.add(banknotes[i]); | |
// the hack: increment counter to reduce the loops | |
counter += banknotes[i]; | |
} | |
} | |
counter++; | |
} | |
// the total sum of the basket | |
// int total = sum(basket); | |
// System.out.println(total); | |
// System.out.println(basket); | |
for (Integer banknote:basket) { | |
if (!map.containsKey(banknote)){ | |
map.put(banknote, 1); | |
} else { | |
map.put(banknote, map.get(banknote) +1); | |
} | |
} | |
//System.out.println(map); | |
for (Map.Entry<Integer, Integer> entry : map.entrySet()) { | |
System.out.println("Banknote: " + entry.getKey() + ", Occurs: " + entry.getValue() + " times"); | |
} | |
System.out.println("Total amount: " + sum(basket) ); | |
} | |
public static Integer sum(List<Integer> list) | |
{ | |
Integer sum = 0; | |
for (Integer i:list) { | |
sum = sum + i; | |
} | |
return sum; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment