algorithm - 用固定值填充 n 个位置的方法

标签 algorithm combinations permutation

有多少种方法可以用 2 个固定值填充 n 个位置,并用 2 个选定值之间的值填充其余位置,这样我们就不会重复?

示例:n=4,固定值 1 和 3

case 1 : fix 1 on position 1 and 3 on position 2

1 3 1 1
1 3 1 2
1 3 2 1
1 3 2 2
1 3 1 3
1 3 3 1
1 3 3 3
1 3 2 3
1 3 3 2

case 2 : fix 1 on position 1 and 3 on position 3

1 1 3 1
1 1 3 2
1 2 3 1
1 2 3 2
1 3 3 2
1 2 3 3
1 3 3 1
1 1 3 3
1 3 3 3
now in case 1 and 2 : 1 3 3 3 and 1 3 3 1 and 1 3 3 2 are repeating 

case 3 : ....similarly other cases follow

到目前为止我所做的是:nC2 * POWER( (max-min+1) , n-2 ) - duplicates 但无法减去重复项。

最佳答案

我不确定是否理解,但如果我理解得很好,你有 n(数字的数量)和适用的数字范围。 x 是该范围内的数字个数。

您必须将此视为对基数 x 的计数。

例子:

x = 3 (we really don't care about min and max it change nothing)
n = 4

1 1 1 1
1 1 1 2
1 1 1 3
1 1 2 1
1 1 2 2


etc.

所以填充的数量应该是:

POWER(x, n)-POWER(x, n-1)

当 x 是你所说的 max-min+1

对于您的示例,他们有 54 种不同的填充方式

关于algorithm - 用固定值填充 n 个位置的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19232873/

相关文章:

c++ - 使用 STL 列出特定子集

python - Python中三个向量元素的所有可能组合

python - 如何在非此即彼的情况下提供一组值?

r - 是否有一个 R 函数可以删除列表中的一个元素并将所有元素存储在另一个列表中

c - 如何展示更好的平均水平?

c# - 生成一手扑克牌的所有不同的 7 张牌组合?

java - 将每个组合排列为字母

performance - 用 Lexigraphic 的重复计算排列

algorithm - 如何以这种方式用最少数量的颜色给车上色——没有水平和垂直线包含两个相同颜色的车?

algorithm - 伪随机目录树生成?