如何确定树中的逐个元素(比赛括号)? [英] How to determinate round by element in tree (Tournament brackets)?

查看:259
本文介绍了如何确定树中的逐个元素(比赛括号)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们有以下树:

  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轮



我如何确定shuch树中的元素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


解决方案

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

在这种情况下,它只是数字的对数,向下舍入。

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

(注意, reverse_match_round 但是,因为我们从 total_rounds 中减去它,所以比起1-index它更容易保持它,如果你喜欢它1-indexed,添加 +1 到最后两行。)


Assume we have following tree:

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

Where elements(matches):
1-8 is round 1
9-12 is round 2
13-14 is round 3
15 is round 4

How I can determinate round of element "n" in shuch tree?

My current formulas are:

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

解决方案

Imagine the tree was numbered in reverse.

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

In that case, it'd simply be the logarithm of the number, rounded down. Now we simply subtract this number from the number of rounds, and we're done.

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

(Note, reverse_match_round is actually 0-indexed, unlike match_round. However, since we subtract it from total_rounds, it's easier to keep it that way than to 1-index it. If you prefer it 1-indexed, simply add +1 to each of the last two lines.)

这篇关于如何确定树中的逐个元素(比赛括号)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆