gpt4 book ai didi

c# - 是否有 c# 相当于 pythons itertools.combinations_with_replacement

转载 作者:太空宇宙 更新时间:2023-11-03 15:17:02 25 4
gpt4 key购买 nike

我正在寻找掷 5 个六面骰子的所有可能方法。我知道在 python 中你可以使用 itertools 来做到这一点,在 c# 中有什么东西可以实现同样的目的吗?

itertools.combinations_with_replacement(iterable, r)

for i in itertools.combinations_with_replacement(range(1, 6), 5)

https://docs.python.org/dev/library/itertools.html#itertools.combinations_with_replacement

范围 1, 6 是骰子的面数, , 5 是掷骰子的数量。想要产生所有 7776 种掷骰子的方式。例如初始滚动可能如下所示:

骰子 1,骰子 2,骰子 3,骰子 4,骰子 5 = 1,2,3,4,5

最佳答案

这很简单 - 它基本上是一个 cross join介于 1-6 5 次之间。

var range = Enumerable.Range(1,6);
var result = from d1 in range
from d2 in range
from d3 in range
from d4 in range
from d5 in range
select new { d1,d2,d3,d4,d5 };

实例:http://rextester.com/VKA17045

关于c# - 是否有 c# 相当于 pythons itertools.combinations_with_replacement,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38719969/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com