Je veux obtenir tous les moyens possibles de faire un certain prix en utilisant quatre dénominations de base (1,5,10,25). J'ai le code suivant. Je sais qu'il génère les résultats dans une collection mais je ne sais pas comment extraire les entiers à l'exécution. Quelqu'un peut-il aider?ne peut pas convertir le type System.Collections.Generic.Icollection <int> en int
void CoinCalculator()
{
//function that checks how many coins and of what denomation the player needs
//get a copy of the purse contents
priceChecker = ApplicationManager.am_keyPrice; //hold key Price
List<ICollection<int>> coins = new List<ICollection<int>>();
coins.Add(CoinChange1.GetCoinSets(priceChecker)[0]);
}
public class CoinChange1
{
private int[] cs = new [] {25, 10, 5, 1};
private List<ICollection<int>> solutions = new List<ICollection<int>>();
public static IList<ICollection<int>> GetCoinSets(int total) {
// Handle corner case outside of recursive solution
if (total == 0)
return new List<ICollection<int>>();
// Get all possible sets
CoinChange1 cc = new CoinChange1();
cc.GetCoinSets (total, 0, new Stack<int>());
return cc.solutions;
}
private void GetCoinSets(int n, int csi, Stack<int> combo) {
// Find largest allowable coin (exploiting that cs is ordered descendingly)
while (cs[csi] > n)
csi++;
int c = cs [csi];
combo.Push (c); // Track coin selection
if (n == c)
solutions.Add(combo.ToArray()); // Base case
else
GetCoinSets (n - c, csi, combo); // Recursion 1: with a selected coin
combo.Pop();
// Recurse for all other possibilities beyond this point with a combo of smaller coin units
if(csi < (cs.Length - 1))
GetCoinSets (n, csi + 1, combo);
}
}
facile, ne pas utiliser ICollection parce qu'il suce. –
Will