gpt4 book ai didi

javascript - 我应该如何用 Javascript 解决这个组合场景?

转载 作者:行者123 更新时间:2023-11-30 00:26:55 25 4
gpt4 key购买 nike

对于至少有 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.我应该如何解决这个问题?

最佳答案

看来你喜欢努力工作:)有一个更简单的方法(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 个(= 一场比赛)。然后,我们将 block 划分为真正的比赛(2 个团队数组)和剩下的(1 个团队数组)。

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

JSbin 更精细,也可以将匹配项转换为团队名称。

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

关于javascript - 我应该如何用 Javascript 解决这个组合场景?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31094089/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com