【问题标题】:ways to fill n places with fixed values用固定值填充 n 个位置的方法
【发布时间】:2013-10-07 19:22:01
【问题描述】:

有多少种方法可以用 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,但无法减去重复项。

【问题讨论】:

  • 能否请说明您正在考虑多少个案例以及它们是如何定义的?也许您必须考虑 2 个固定值在任何 2 个位置的所有可能定位? ..或者要使用的案例和职位的数量作为单独的数据给出?

标签: algorithm combinations permutation


【解决方案1】:

我不确定是否理解,但如果我理解得很好,您有 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 种不同的填充方式

【讨论】:

    猜你喜欢
    • 2012-04-02
    • 1970-01-01
    • 1970-01-01
    • 2018-05-21
    • 2020-06-02
    • 1970-01-01
    • 2019-10-29
    • 1970-01-01
    • 2016-07-11
    相关资源
    最近更新 更多