You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过回合ID与淘汰制总席位获取对应淘汰赛回合名称?

修复后的淘汰赛回合名称函数

原函数存在以下几个核心问题:

  • totalRounds 用 Math.round 计算会产生误差,淘汰赛席位 slots 必然是2的幂,应使用 Math.log2 直接获取准确总轮次
  • currentRoundTeams 计算逻辑错误,原代码的 slots / thisRoundId 会在 thisRoundId=0 时触发除以0错误,正确逻辑应为当前轮队伍数 = 总席位 / (2的当前轮ID次方)
  • 存在语法错误:slots / thisRoundId ??; 是无效代码

以下是修复后的函数,可实现你需要的「XX强赛」「四分之一决赛」「半决赛」「决赛」命名逻辑:

export function getKnockoutRoundName(
  thisRoundId: number, // 从零开始
  slots: number,
): string {
  // 计算总轮次,slots需为2的幂(符合淘汰赛规则)
  const totalRounds = Math.log2(slots);
  const isLastRound = thisRoundId === totalRounds - 1;
  
  if (isLastRound) {
    // 决赛轮包含季军赛
    return "决赛";
  }
  
  // 计算当前轮的队伍数量
  const currentRoundTeams = slots / (2 ** thisRoundId);
  
  // 根据队伍数返回对应名称
  if (currentRoundTeams >= 16) {
    return `${currentRoundTeams}强赛`;
  } else if (currentRoundTeams === 8) {
    return "四分之一决赛";
  } else if (currentRoundTeams === 4) {
    return "半决赛";
  }
  
  // 兜底处理(理论上不会触发,因slots是2的幂)
  return `Round ${currentRoundTeams}`;
}

逻辑验证(以slots=256为例)

  • thisRoundId=0 → 256/(2^0)=256 → 返回「256强赛」
  • thisRoundId=1 → 256/(2^1)=128 → 返回「128强赛」
  • thisRoundId=2 → 64 → 返回「64强赛」
  • thisRoundId=3 → 32 → 返回「32强赛」
  • thisRoundId=4 → 16 → 返回「16强赛」
  • thisRoundId=5 → 8 → 返回「四分之一决赛」
  • thisRoundId=6 →4 → 返回「半决赛」
  • thisRoundId=7 → 最后一轮 → 返回「决赛」

如果需要保留英文命名(如原代码的"Quarter""Semi"),只需替换对应返回值:

  • 8强对应 "Quarter Finals"
  • 4强对应 "Semi Finals"
  • 决赛对应 "Final"
  • 16强及以上对应 ${currentRoundTeams}-Round

内容的提问来源于stack exchange,提问作者RedGiant

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.24 23:53:14