Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.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# 所有可能的现在给予排列的集合_C#_Linq_Permutation - Fatal编程技术网

C# 所有可能的现在给予排列的集合

C# 所有可能的现在给予排列的集合,c#,linq,permutation,C#,Linq,Permutation,所以圣诞节就要到了,每年我的家人都会从帽子上取名字,告诉谁应该给谁买礼物,而且总是会有人担心,主要是关于配偶为彼此买礼物 假设这些族是这样的: List<List<string>> families = new List<List<string>> () { new List<string>() { "A1", "A2" }, new List<string>() { "B1", "B2" },

所以圣诞节就要到了,每年我的家人都会从帽子上取名字,告诉谁应该给谁买礼物,而且总是会有人担心,主要是关于配偶为彼此买礼物

假设这些族是这样的:

List<List<string>> families = new List<List<string>> () 
{
    new List<string>() { "A1", "A2" }, 
    new List<string>() { "B1", "B2" }, 
    new List<string>() { "C1", "C2" }, 
    new List<string>() { "D1", "D2" }
};

我如何才能将其转化为包括所有人在内的有效给予者/接受者的可能排列集合?从中,我将选择一个随机集合。

我编写了一些代码来解决您的问题,但它有时会抛出一个异常,当它在选择配对时有点“不走运”。例如,如果算法对A1B2 B1C2 C1A2->因此只剩下D1和D2,这会导致异常,因为它不再满足您的配对要求

无论如何,下面是代码,您可能希望对其进行扩展以防止其引发异常:

        var everyone = families.SelectMany(family => family).ToList();
        everyone.Shuffle();
        var randPairs = families.SelectMany(family => family)
            .Select(p => new { 
                Giver = p, 
                Receiver = everyone.PopRandom(x => !p.Contains(x[0])) 
            });
以及IList的两种扩展方法:

    public static T PopRandom<T>(this IList<T> list, Func<T, bool> predicate)
    {
        var predicatedList = list.Where(x => predicate(x));
        int count = predicatedList.Count();
        if (count == 0)
        {
            throw new Exception();
        }
        T item = predicatedList.ElementAt(Rand.Next(count));
        while (item != null && !predicate(item))
        {
            item = predicatedList.ElementAt(Rand.Next(list.Count));
        }
        list.Remove(item);

        return item;
    }

    public static void Shuffle<T>(this IList<T> list)
    {
        int n = list.Count;
        while (n > 1)
        {
            n--;
            int k = Rand.Next(n + 1);
            T value = list[k];
            list[k] = list[n];
            list[n] = value;
        }
    }
publicstaticpoptrandom(此IList列表,Func谓词)
{
var predicatedList=list.Where(x=>predicate(x));
int count=predictedlist.count();
如果(计数=0)
{
抛出新异常();
}
T item=predictedlist.ElementAt(Rand.Next(count));
while(item!=null&&!谓词(item))
{
item=predictedlist.ElementAt(Rand.Next(list.Count));
}
列表。删除(项目);
退货项目;
}
公共静态无效洗牌(此IList列表)
{
int n=list.Count;
而(n>1)
{
n--;
int k=下一个随机数(n+1);
T值=列表[k];
列表[k]=列表[n];
列表[n]=值;
}
}

< /代码>我会把这个模型化为一个图形问题。人是有向图中的节点。箭头是“可以购买”的关系。为所有节点构造k-完全图,然后删除表示“非法”对的边。现在,为图中的每个节点选择一个出站箭头;执行此操作时,删除原始节点中的所有其他出站箭头,以及所选节点中的所有入站箭头。存在无人指向或无人指向的节点的配置是非法的;拒绝它。
        var everyone = families.SelectMany(family => family).ToList();
        everyone.Shuffle();
        var randPairs = families.SelectMany(family => family)
            .Select(p => new { 
                Giver = p, 
                Receiver = everyone.PopRandom(x => !p.Contains(x[0])) 
            });
    public static T PopRandom<T>(this IList<T> list, Func<T, bool> predicate)
    {
        var predicatedList = list.Where(x => predicate(x));
        int count = predicatedList.Count();
        if (count == 0)
        {
            throw new Exception();
        }
        T item = predicatedList.ElementAt(Rand.Next(count));
        while (item != null && !predicate(item))
        {
            item = predicatedList.ElementAt(Rand.Next(list.Count));
        }
        list.Remove(item);

        return item;
    }

    public static void Shuffle<T>(this IList<T> list)
    {
        int n = list.Count;
        while (n > 1)
        {
            n--;
            int k = Rand.Next(n + 1);
            T value = list[k];
            list[k] = list[n];
            list[n] = value;
        }
    }