算法计算的组合的数量,以形成100组合、算法、数量

2023-09-10 23:55:28 作者:别想逃离我的贼船

我打一个棘手的情况下,我需要计算组合的数量根据不同的因素,形成100。

I am struck in a tricky situation where I need to calculate the number of combinations to form 100 based on different factors.

这些都是

的组合数 倍增因子 在距离

输入示例1:(20年2月10日)

这意味着

列出了有效的双向结合,形成100 的结合间的距离应小于或等于20。 和所有的最终组合必须能整除给定倍增系数10

输出为

[40,60]

[50,50]

[60,40]

在这里[30,70],[20,60]是无效的,因为距离大于20。

here [30,70],[20,60] are invalid because the distance is above 20.

采样输入2: [20年2月5日]

[40,60]

[45,55]

[50,50]

[55,45]

[60,40]

我真的AP preciate如果您指引我正确的方向。

I would really appreciate if you guided me to the right direction.

干杯。

推荐答案

我希望这不是一个家庭作业的问题!

I hope it's not a homework problem!

    def combinations(n: Int, step: Int, distance: Int, sum: Int = 100): List[List[Int]] =
      if (n == 1) 
        List(List(sum))
      else 
        for {
          first <- (step until sum by step).toList
          rest <- combinations(n - 1, step, distance, sum - first)
          if rest forall (x => (first - x).abs <= distance)
        } yield first :: rest