Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/267.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 无法转换类型System.Collections.Generic.Icollection<;int>;整型_C#_Icollection - Fatal编程技术网

C# 无法转换类型System.Collections.Generic.Icollection<;int>;整型

C# 无法转换类型System.Collections.Generic.Icollection<;int>;整型,c#,icollection,C#,Icollection,我想用四种基本面额(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 = Applic

我想用四种基本面额(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);
    }
}
void coincollator()
{
//用于检查玩家需要多少硬币和什么符号的函数
//拿一份钱包里的东西
priceChecker=ApplicationManager.am_keyPrice;//保留密钥价格
列表硬币=新列表();
coins.Add(CoinChange1.GetCoinSets(priceChecker)[0]);
}
公共类CoinChange1
{
私有int[]cs=new[]{25,10,5,1};
私有列表解决方案=新列表();
公共静态IList GetCoinSets(整数总计){
//在递归解之外处理角点情况
如果(总计==0)
返回新列表();
//获取所有可能的集合
CoinChange1 cc=新的CoinChange1();
cc.GetCoinSets(总计,0,新堆栈());
返回cc.solutions;
}
私有void getcoinset(int n、int csi、堆栈组合){
//找到允许的最大硬币(利用cs按下降顺序排列)
while(cs[csi]>n)
csi++;
int c=cs[csi];
combo.Push(c);//跟踪硬币选择
如果(n==c)
solutions.Add(combo.ToArray());//基本情况
其他的
GetCoinSets(n-c,csi,combo);//递归1:使用选定的硬币
combo.Pop();
//使用较小的硬币单位组合,在该点以外的所有其他可能性中递归
如果(csi<(cs.长度-1))
GetCoinSets(n,csi+1,combo);
}
}

您有一个集合列表,以便将它们输出到控制台,例如:

foreach(ICollection<int> coll in solutions)(
{
     foreach(int item in coll)
     {
          Console.WriteLine(item);
     }
}
foreach(解决方案中的ICollection coll)(
{
foreach(coll中的int项)
{
控制台写入线(项目);
}
}

因为您有集合列表,所以必须迭代列表,并且对于列表中的每个项目,都要迭代集合以获得整数。

您可以迭代结果并“打印”它们,或者执行任何您想要的操作。例如

        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}");
        }

简单,不要使用ICollection,因为它很糟糕。感谢您的快速回复!我添加了以下行以传递给数组,它起了作用…int[]coins=CoinChange1.GetCoinSets(priceChecker)[0]。ToArray();将列表强制转换为int(int[])数组真的非常有用,它们可以以相同的方式进行迭代。非常感谢!非常感谢。
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