【问题标题】:How should I solve this combinations scenario with Javascript?我应该如何使用 Javascript 解决这种组合方案?
【发布时间】:2015-06-27 22:16:39
【问题描述】:

对于最少 8 支球队最多 18 支球队的锦标赛,我必须确定比赛日历。比赛有17个回合或比赛日。所以每支球队必须在每个比赛日遇到另一支球队。如果少于 18 支队伍可以重复遭遇战,那么一支队伍可以与另一支队伍多次交手。

This is an example for 18 teams tournament.And this would be a case for less than 18 teams fixture, here in particular 9 teams.

所以,我必须进行排列,然后将它们排列在不同的轮次中。我试过了:

组合:

function k_combinations(set, k) {
    var i, j, combs, head, tailcombs;

    if (k > set.length || k <= 0) {
        return [];
    }

    if (k == set.length) {
        return [set];
    }

    if (k == 1) {
        combs = [];
        for (i = 0; i < set.length; i++) {
            combs.push([set[i]]);
        }
        return combs;
    }

    combs = [];
    for (i = 0; i < set.length - k + 1; i++) {
        head = set.slice(i, i+1);
        tailcombs = k_combinations(set.slice(i + 1), k - 1);
        for (j = 0; j < tailcombs.length; j++) {
            combs.push(head.concat(tailcombs[j]));
        }
    }
    return combs;
}

var teams = [   {name: 'Real Madrid'},
                {name: 'Las Palmas'},
                {name: 'Alavés'},
                {name: 'Valencia'},
                {name: 'Sevilla'},
                {name: 'Betis'},
                {name: 'Córdoba'},
                {name: 'Deportivo'},
                {name: 'Atlético de Madrid'},
                {name: 'Levante'},
                {name: 'Rayo Vallecano'},
                {name: 'Athletic Bilbao'},
                {name: 'Osasuna'},
                {name: 'Zaragoza'},
                {name: 'Villareal'},
                {name: 'Racing de Santander'},
                {name: 'Espanyol'},
                {name: 'Cádiz'},
                ];
// Compute whole encounters combinations.
var seasonMatches = k_combinations(teams,2);

组合的回合排列:

var calendar = {};
for (var i = 0; i<17; i++) {
    calendar[i+1] = [];
}
var encounters = seasonMatches;

for (var i = 0; i<Object.keys(calendar).length; i++) {

    encounters.map(function (match,index) {

        if (! _.any(calendar, function (m) {

           return m[0].name === match[0].name || m[1].name === match[1].name || m[0].name === match[1].name || m[1].name === match[0].name;
        })) {
            calendar[i+1].push(match);
        }
    });
}

我正在使用 lodash 来简化对上一轮遭遇的存在性检查。

我遇到的问题是,这样我在日历中的每一轮都会遇到相同的情况。而且,如果我在 seasonMatches 中添加一个拼接,我最终会得到每轮不同的匹配。

I've got a fiddle with this example shown above. 我应该如何解决这个问题?

【问题讨论】:

    标签: javascript combinations lodash


    【解决方案1】:

    看来你喜欢努力工作:) 还有更简单的方法 (jsbin link):

    var teamsCount = 9;
    var matchDays = 17;
    var matches = [];
    var teams = _.shuffle(_.range(teamsCount));
    while(matches.length < matchDays){
      var newMatches = _(teams).chunk(2).partition(function(match){
        return match.length === 2;
      }).value();
      matches = matches.concat(newMatches[0]);
      if(newMatches[1].length) { // one team was left out, let's make sure it is playing
        // we put it first, and add the other teams, shuffled, without that one team
        teams = newMatches[1][0].concat(_.shuffle(_.without(_.range(teamsCount), newMatches[1][0][0])));
      } else {
        teams = _.shuffle(_.range(teamsCount));
      }
    }
    
    // we might get more then we need
    matches = _.take(matches, matchDays);
    
    _.each(matches, function(match, index){
      console.log('round ' + index + ': ' + match);
    });
    

    说明: 由于您没有施加其他限制(例如,每支球队都必须互相比赛),因此只需将球队带入,洗牌并一次将它们分成 2 个(= 一场比赛)。然后,我们将块划分为真实匹配(2 个团队数组)和剩余部分(1 个团队数组)。

    我们采用真实匹配并将它们添加到现有匹配中。如果我们有剩下的,我们保留它,并将洗牌的团队(没有剩下的团队)连接到它并再次分块。我们继续,直到我们有足够的匹配。因为我们可能会得到比我们需要的更多的匹配,所以我们只选择前 17 个。

    JSbin 更加精细,并且还将比赛转换为团队名称。

    我试图查看您的代码以了解为什么您会得到所显示的模式,但这对我来说太复杂了无法理解,我喜欢以简单的方式做事;-)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-28
      • 1970-01-01
      • 1970-01-01
      • 2014-05-21
      相关资源
      最近更新 更多