【问题标题】:Find All Possible Outcomes of all Matches in a Football Tournament查找足球锦标赛中所有比赛的所有可能结果
【发布时间】:2020-10-08 20:35:04
【问题描述】:

我正在处理一个问题陈述,我需要在其中返回足球锦标赛中所有可能的结果组合的列表。

让我们假设一场比赛的结果可以是 1 - 第一队获胜,0 - 第二队获胜

public ArrayList<Result> calculate(List<Matches> list){

    if(list.size == 0){
    return;
  } else {
    Match M = list.get(0);
    list.remove(0); 
    calculate(list);

  }
 }

我正在尝试使用递归方法。

输出应该是所有匹配的所有可能结果的列表。这意味着如果总共要进行 4 场比赛,那么将这四场比赛中所有可能的结果组合加起来

【问题讨论】:

  • 你的问题是什么?你会添加更多细节吗? :) 顺便说一句,你打算算平局吗?
  • 目前,我不考虑抽奖
  • 很遗憾,用粗体字不会让您的问题更清楚。你如何定义你的 Match 和 Result 类?您是否至少详细说明了一个最简单的情况,即仅提供 1 个匹配项 - 您应该返回这个给定匹配的结果,还是返回给定匹配的所有个可能结果?当提供 2 个匹配项时,您希望看到什么?

标签: java recursion permutation


【解决方案1】:

我不确定您的问题,但如果您尝试使用递归方法返回列表,一般的想法是:

public ArrayList<Result> calculate(List<Matches> list){

  if(list.size == 0){
    return new ArrayList<Result>();
  } else {
    Match m1 = list.get(0);
    list.remove(0); // If you want to include a team against itself, move this to after the for loop
    ArrayList<Result> res = new ArrayList<Result>();
    for(Match m2 : list){
      //I'm not sure how you're comparing matches, but just as an example:
      res.add(m1.compare(m2));
    }
    return res.addAll(calculate(list));
  }
}

在这里,我们从列表中取出最顶部的匹配项,并将其与列表中的所有其他匹配项进行比较,然后递归调用其余匹配项。我真的不确定你的问题到底是什么,因为它的措辞不是很清楚,但我希望这至少会有所帮助。

【讨论】:

    猜你喜欢
    • 2019-03-22
    • 2013-11-18
    • 1970-01-01
    • 1970-01-01
    • 2022-07-11
    • 1970-01-01
    • 1970-01-01
    • 2017-05-21
    • 1970-01-01
    相关资源
    最近更新 更多