- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在尝试构建一个接受输入的 Javascript 函数:
["player1","player2","player3","player4"]
(可能只有相同数量的玩家)并根据以下规则为锦标赛设计动态创建数组:
输出将是一个包含匹配数组的数组,每个匹配项有四个条目,例如[player1, player2, player3, player4] 代表player1和player2对player3和player4。
[["player1","player2","player3","player4"], ["player1","player3","player2","player4"], ...]
目前,我使用类似下面示例的方法来执行此硬编码,但不幸的是,仅适用于预定义数量的玩家。
const m = [];
const A = players[0];
const B = players[1];
const C = players[2];
const D = players[3];
const E = players[4];
const F = players[5];
const G = players[6];
const H = players[7];
const I = players[8];
const J = players[9];
const K = players[10];
const L = players[11];
const M = players[12];
const N = players[13];
const O = players[14];
const P = players[15];
m.push(A, B, C, P);
m.push(A, C, E, O);
m.push(B, C, D, A);
m.push(B, D, F, P);
m.push(C, D, E, B);
m.push(C, E, G, A);
m.push(D, E, F, C);
m.push(D, F, H, B);
m.push(E, F, G, D);
m.push(E, G, I, C);
m.push(F, G, H, E);
m.push(F, H, J, D);
m.push(G, H, I, F);
m.push(G, I, K, E);
m.push(H, I, J, G);
m.push(H, J, L, F);
m.push(I, J, K, H);
m.push(I, K, M, G);
m.push(J, K, L, I);
m.push(J, L, N, H);
m.push(K, L, M, J);
m.push(K, M, O, I);
m.push(L, M, N, K);
m.push(L, N, P, J);
m.push(M, N, O, L);
m.push(M, O, A, K);
m.push(N, O, P, M);
m.push(N, P, B, L);
m.push(O, P, A, N);
m.push(O, A, C, M);
m.push(P, A, B, O);
m.push(P, B, D, N);
return m;
感谢每一个提示!
干杯
最佳答案
您可以使用 round-robin tournament配对玩家的机制。在每次迭代中,除一个玩家外,所有玩家都会取代下一个玩家。如果玩家数量为奇数,则会有一名玩家被排除在匹配之外,但每次迭代都是不同的。由于一个游戏需要 2 对,因此可能有一对也没有参与。同样,这将是每次迭代中的不同对。
此方法将使每个玩家与其他玩家玩同样多的游戏,但玩家数量为 2 模 4 时除外(即 6、10、14 等)。在这种情况下,除一个玩家外,所有玩家都将玩相同数量的游戏。杰出的球员将再打 2 场比赛。
为 n 个玩家找到的游戏数量,以及每个玩家的游戏数量,将遵循以下公式:
#players(n) modulo 4 | #games | #games per player
----------------------+----------------------+--------------------
0 | n(n-1)/4 | n-1
1 | n(n-1)/4 | n-1
2 | (n-1)(n-2)/4 | n-3 (one: n-1)
3 | floor((n-1)(n-2)/4) | n-3
示例:给定 16 名玩家,该算法将找到 60 场比赛,其中每位玩家可以参加 15 场比赛。
这是一个实现:
function assignToGames(players) {
// Round the number of players up to nearest multiple of 2.
// The potential extra player is a dummy, and the games they play
// will not be included.
const numPlayers = players.length + players.length % 2, // potential dummy added
pairsPerRound = numPlayers / 2,
rotatingPlayers = numPlayers - 1,
firstRound = players.length % 2, // will make the dummy game being ignored
games = [];
for (let round = 0; round < rotatingPlayers; round++) {
for (let i = firstRound; i < pairsPerRound-1; i+=2) {
// The following formulas reflect a roundrobin scheme, where
// the last player (possibly a dummy) does not move.
games.push([
players[i ? (i+round-1) % rotatingPlayers : numPlayers - 1],
players[(numPlayers-i-2+round) % rotatingPlayers],
players[(i+round) % rotatingPlayers],
players[(numPlayers-i-3+round) % rotatingPlayers],
]);
}
}
return games;
}
// Optional function to test the correctness of the result,
// and count the number of games per player:
function getStatistics(players, games) {
const usedPairs = new Set(),
stats = Object.assign(...players.map( player => ({ [player]: 0 }) ));
for (let game of games) {
// verify uniqueness of pairs
for (let pairIndex = 0; pairIndex < 4; pairIndex += 2) {
let pair = JSON.stringify(game.slice(pairIndex,pairIndex+2).sort());
if (usedPairs.has(pair)) throw "Duplicate pair " + pair;
usedPairs.add(pair);
}
}
// Count the number of games each player plays:
for (let i = 0; i < games.length; i++) {
for (let j = 0; j < 4; j++) {
stats[games[i][j]]++;
}
}
return stats;
}
// Demo
// Create 16 players. Their values are the letters of the alphabet up to "p".
const players = Array.from("abcdefghijklmnop");
const games = assignToGames(players);
// Display results
console.log(JSON.stringify(games));
console.log("--- statistics ---");
console.log('#games: ', games.length);
const stats = getStatistics(players, games);
console.log(stats);
关于javascript - 配对锦标赛设计算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47052319/
我有一个服务器程序(适用于所有三个主要操作系统),它是用 C++ 创建的,它努力连接两个陌生人进行通信。我当前的模型似乎已经过时了,我只是想知道是否有更好的方法来为客户提供服务。 -服务器收到连接请求
我希望有人能告诉我 GoogleTV 如何与本地 Youtube 应用配对。 我感兴趣的是“第一屏”GoogleTV/SmartTV 功能。 据我目前所知,“第二屏”应用程序需要使用与配对服务(来自
感谢下面的提示 • 您需要一次遍历字符串一个字符(for 循环或 while 循环) 当您点击 ,这是您的结束标记 • 现在检查> 之前的字符。是/吗? • 是:查看堆栈顶部。该字符串与 之间的字符
我正在使用 Node.js 和 WebSocket 创建基本的一对一聊天。每次客户端连接时,都会向他们发送其 ID 以及 salt+id 的 MD5 哈希值。然后,他们需要与另一个客户配对。当它们配对
我刚刚开始深入研究 Racket 宏,并尝试制作一个简洁的宏定义宏。我想扩展这样的表达式: (macro id (param) replacement1 (params ...) re
我有一个 foreach 循环(看,它在下面),我得到了一些元素。将此数据存储到 MySQL 中时,我想选择每个“count[]”所属的类别(此处标记为“interaction[]”)。例如,如果第
是否有可能找到一种更好的/模块化的方式来为配对分配相同的颜色,而不是像我目前实现的那样对它们进行硬编码? 如果对象 fname 匹配,则分配相同的颜色。 以下是 javascript 对象的子集。 d
在我的 iPhone 应用程序中,我想与固件设备通信。在连接期间,他们可以要求提供配对 key ,但他们说他们没有提供输入 key 的规定。 在这种情况下,USP(用户智能手机)永远不知道输入的 ke
这个问题在这里已经有了答案: How to merge every two lines into one from the command line? (21 个回答) 关闭 6 年前。 使用一个简
function pair(str) { var dna = []; var dnaarr = []; for(var i = 0; i < str.length; i++) {
我是一个相对年轻的开发人员,我对一些事情感到困惑。 这是我的代码: function pairElement(str) { var arr = []; var pairs = [
我有一个脚本,可以从文本文件中读取并插入元素,以便可以对它们进行样式设置和显示。但是,我现在想在 DIV 中配对两个元素。这是代码: var lines = request.responseText.
.Spotify:hover img { display:block; position: absolute; z-index: 0; top:17%; lef
我正在努力使用摩托罗拉随 RAZR 提供的 BT 4.0 API。在 one of their documents它声明在连接和使用他们的框架之前使用 Android API 进行配对。根据他们的说明
谁能告诉我一次可以通过蓝牙将多少台设备与 iPhone 配对..需要帮助..直到现在我还没有确切的数字。 最佳答案 在 iOS 6.1.4 中,低功耗蓝牙设备的当前限制是 10 个同时连接(至少在 N
我正在创建一个扩展推送通知的 Android Wear 应用程序。当推送通知进来时,我的应用程序会从服务器下载大约 10 张图像,并在 watch 上显示这些额外的图像。这些图像特定于 android
关闭。这个问题需要details or clarity .它目前不接受答案。 想改进这个问题?通过 editing this post 添加详细信息并澄清问题. 8年前关闭。 Improve this
我有一个类似于使用以下命令获得的数据表: dt <- data.table( time = 1:8, part = rep(c(1, 1, 2, 2), 2), type = rep(c(
为了回应有用的评论,我编辑了原始问题(我假设 for 循环和应用循环给出不同的结果)。 我正在使用 R 运行大量 2 组 t 检验,使用来自分隔表的输入。根据此处和其他地方的建议,我尝试了“for-l
是否可以将脚本(例如 MathJax)加载到 EpicEditor 预览 iFrame 中?我希望我的预览是正确的 Markdown,然后运行 javascript 来预览 MathJax 内容。
我是一名优秀的程序员,十分优秀!