Last active
August 29, 2015 13:56
-
-
Save MattRix/8950572 to your computer and use it in GitHub Desktop.
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
using UnityEngine; | |
using System; | |
using System.Collections.Generic; | |
public class DenomMaker | |
{ | |
public static List<int> GetDenoms(int[] denoms, int totalValue, int numCoins) | |
{ | |
List<int> resultCoins = new List<int>(numCoins+10); | |
if(totalValue <= 0) return resultCoins; | |
numCoins = Math.Max(1,Math.Min(totalValue,numCoins)); //can't make more coins than we have value for! | |
int valueLeftToAdd = totalValue; | |
int numCoinsLeft = numCoins; | |
int denomCount = denoms.Length; | |
while(numCoinsLeft > 0 && valueLeftToAdd > 0) | |
{ | |
int avg = valueLeftToAdd/numCoinsLeft; | |
int pickedCoin = -1; | |
for(int d = 0; d<denomCount; d++) | |
{ | |
if(denoms[d] > avg) | |
{ | |
pickedCoin = denoms[d-1]; | |
break; | |
} | |
} | |
if(pickedCoin == -1) | |
{ | |
pickedCoin = denoms[denomCount-1]; | |
} | |
resultCoins.Add(pickedCoin); | |
valueLeftToAdd -= pickedCoin; | |
numCoinsLeft--; | |
if(numCoinsLeft == 0 && valueLeftToAdd > 0) | |
{ | |
numCoinsLeft++; | |
} | |
} | |
return resultCoins; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment