如何确定的一轮元素树(比赛括号内)?

编程入门 行业动态 更新时间:2024-10-15 18:24:53
本文介绍了如何确定的一轮元素树(比赛括号内)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

假设我们有以下三种:

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

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

我怎么能确定的在shuch树轮的元素N?

我目前的公式是:

total_rounds =地板(日志(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      五 10          2 9      4 8

在这种情况下,它会简单地将数的对数,四舍五入。现在,我们只需要减去此数轮数,我们就大功告成了。

reverse_number = total_matches - match_number + 1; reverse_match_round =地板(日志(reverse_number,2)); match_round = total_rounds - match_round;

(注意, reverse_match_round 实际上是0索引,不像match_round。但是,由于我们从 total_rounds 减去它,它更容易保持这种方式比1索引它。如果你preFER这1索引,只需添加 +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.)

更多推荐

如何确定的一轮元素树(比赛括号内)?

本文发布于:2023-11-30 02:09:35,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1648371.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:括号内   元素

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!