无法将类型 System.Collections.Generic.Icollection<int> 转换为 int
cannot convert type System.Collections.Generic.Icollection<int> to int
我想获得使用四种基本面额 (1,5,10,25) 计算某个价格的所有可能方法。我有以下代码。我知道它会在集合中生成结果,但我不知道如何在运行时提取整数。有人可以帮忙吗?
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);
}
}
你有一个集合列表,以便将它们输出到控制台,例如:
foreach(ICollection<int> coll in solutions)(
{
foreach(int item in coll)
{
Console.WriteLine(item);
}
}
因为您有一个集合列表,所以您必须迭代该列表,并且对于列表中的每个项目迭代该集合以获得您的 int。
您可以只遍历结果并 "print" 它们或做任何您想做的事。例如.
var result = GetCoinSets(priceChecker );
// display result
for (int i = 0; i < result.Count; i++) {
string valuesSeparatedByComma = string.Join(", ", result[i]);
Debug.WriteLine($"Combinaison #{i + 1}: {valuesSeparatedByComma}");
}
如果输入 15: 将显示
Combinaison #1: 5, 10
Combinaison #2: 1, 1, 1, 1, 1, 10
Combinaison #3: 5, 5, 5
Combinaison #4: 1, 1, 1, 1, 1, 5, 5
Combinaison #5: 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 5
Combinaison #6: 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
我想获得使用四种基本面额 (1,5,10,25) 计算某个价格的所有可能方法。我有以下代码。我知道它会在集合中生成结果,但我不知道如何在运行时提取整数。有人可以帮忙吗?
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);
}
}
你有一个集合列表,以便将它们输出到控制台,例如:
foreach(ICollection<int> coll in solutions)(
{
foreach(int item in coll)
{
Console.WriteLine(item);
}
}
因为您有一个集合列表,所以您必须迭代该列表,并且对于列表中的每个项目迭代该集合以获得您的 int。
您可以只遍历结果并 "print" 它们或做任何您想做的事。例如.
var result = GetCoinSets(priceChecker );
// display result
for (int i = 0; i < result.Count; i++) {
string valuesSeparatedByComma = string.Join(", ", result[i]);
Debug.WriteLine($"Combinaison #{i + 1}: {valuesSeparatedByComma}");
}
如果输入 15: 将显示
Combinaison #1: 5, 10
Combinaison #2: 1, 1, 1, 1, 1, 10
Combinaison #3: 5, 5, 5
Combinaison #4: 1, 1, 1, 1, 1, 5, 5
Combinaison #5: 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 5
Combinaison #6: 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1