如何确定轮的元素在树(锦标赛括号)
How to determinate round by element in tree (Tournament brackets)?
假设我们有如下树:
1
9
2
13
3
10
4
15
5
11
6
14
7
12
8
元素(匹配):
1-8是第一轮
9-12是第2轮
13-14是第3轮
我如何确定元素"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
在这种情况下,它只是数字的对数,四舍五入。现在我们只需从轮数中减去这个数,就完成了。
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索引,不像match_round。然而,由于我们从total_rounds
中减去它,保持它的形式比1索引更容易。如果您希望它以1为索引,只需将+1
添加到最后两行。
相关文章:
- 如何确定我已使用非编码文件到达 EOF?
- 函数何时会在c++中包含stack_Unwind_Resume调用
- 如何在C++中确定文本文件中的元素是字符还是数字
- Python中的for循环与C++有何不同
- 在clang++预处理器中确定gcc工具链版本
- 不确定要在我的main中放入什么才能使我的代码正常工作
- C++setiosflags函数操纵器-未确定的缩进
- 编译器如何在使用SFINAE的函数和标准函数之间确定两者是否可行
- 如何使用url确定网站协议
- 通过构造函数动态确定类实现
- 遍历并行数组以确定C++中的最大数字
- C++如果必须在编译时确定大小,std::array 有什么意义?
- 使用不变量来确定二分搜索中的边界条件
- 无论如何,我可以确定构造函数是否存在吗?
- compare_exchange C++函数如何确定竞争条件?
- 如何使用 do while 循环确定最高值和最低值
- 如何确定哪个标头调用 c++ 中的另一个标头
- 在C++中释放内存期间,迭代器与指针有何不同
- 什么是C++中的不确定行为?它与未定义的行为有何不同
- 如何确定轮的元素在树(锦标赛括号)