Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static void waysMakeChangeDifferent(int centsToChange){
- coinCollections = new HashSet<CoinCollection>();
- System.out.println("There are "+waysMakeChangeDifferentRecurse(centsToChange, new CoinCollection())+" different solutions." );
- for (CoinCollection cc:coinCollections){
- System.out.println();
- System.out.println("Num pennies: "+cc.getNumPennies());
- System.out.println("Num nickels: "+cc.getNumNickels());
- System.out.println("Num dimes: "+cc.getNumDimes());
- System.out.println("Num quarters: "+cc.getNumQuarters());
- }
- }
- private static int waysMakeChangeDifferentRecurse(int centsToChange, CoinCollection inCoins){
- if (inCoins.getSum()>centsToChange){
- return 0;
- }
- if (inCoins.getSum()==centsToChange){
- coinCollections.add(inCoins);
- return 1;
- }
- //Check if any of the new coin sets have already been done
- int currQuarters = inCoins.getNumQuarters();
- int currDimes = inCoins.getNumDimes();
- int currNickels = inCoins.getNumNickels();
- int currPennies = inCoins.getNumPennies();
- CoinCollection withNewQuarter = new CoinCollection(currPennies, currNickels, currDimes, currQuarters+1);
- int numFromQuarter = 0;
- if (!coinCollections.contains(withNewQuarter)){//If we haven't already found this value
- numFromQuarter = waysMakeChangeDifferentRecurse(centsToChange, withNewQuarter);
- }
- CoinCollection withNewDime = new CoinCollection(currPennies, currNickels, currDimes+1, currQuarters);
- int numFromDime = 0;
- if (!coinCollections.contains(withNewDime)){
- numFromDime = waysMakeChangeDifferentRecurse(centsToChange, withNewDime);
- }
- CoinCollection withNewNickel = new CoinCollection(currPennies, currNickels+1, currDimes, currQuarters);
- int numFromNickel = 0;
- if (!coinCollections.contains(withNewNickel)){
- numFromNickel = waysMakeChangeDifferentRecurse(centsToChange, withNewNickel);
- }
- CoinCollection withNewPenny = new CoinCollection(currPennies+1, currNickels, currDimes, currQuarters);
- int numFromPenny = 0;
- if (!coinCollections.contains(withNewPenny)){
- numFromPenny = waysMakeChangeDifferentRecurse(centsToChange, withNewPenny);
- }
- return numFromPenny+numFromNickel+numFromDime+numFromQuarter;
- }
Advertisement
Add Comment
Please, Sign In to add comment