【问题标题】:How to determinate round by element in tree (Tournament brackets)?如何确定树中的元素(锦标赛括号)?
【发布时间】:2012-01-27 08:16:13
【问题描述】:

假设我们有以下树:

1
    9
2
        13
3
    10
4 
            15
5
    11
6 
        14
7   
    12
8

元素在哪里(匹配):
1-8是第1轮
9-12是第2轮
13-14是第3轮
15 是第 4 轮

如何确定树中元素“n”的轮次?

我目前的公式是:

total_rounds = floor(log(totalTeams,2));

matches_per_round = (totalTeams / pow(2, current_round))

next_match_id = (totalTeams/2) + ceil(match_id/2)

total_matches = total_teams - 1

【问题讨论】:

  • 如果这是作业,请标记它。

标签: c++ algorithm algebra tournament


【解决方案1】:

想象一下这棵树是倒序编号的。

15
     7
14
         3
13 
     6
12 
             1
11
     5 
10 
         2
9   
     4
8

在这种情况下,它只是数字的对数,向下取整。现在我们只需从轮数中减去这个数字,就完成了。

reverse_number = total_matches - match_number + 1;
reverse_match_round = floor(log(reverse_number, 2));
match_round = total_rounds - match_round;

(注意,reverse_match_round 实际上是 0-indexed,与 match_round 不同。但是,由于我们从 total_rounds 中减去它,因此保持这种方式比 1-indexed 更容易。如果您更喜欢它 1-indexed ,只需将+1 添加到最后两行中的每一行。)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-04-03
    • 2011-01-12
    • 2012-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-24
    • 2016-01-16
    相关资源
    最近更新 更多