- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
如果您在阅读问题后能想出更好的标题,请随时更改。
因此,作为输入,我有一个整数,它是 2 到 20 之间的偶数。我们称这个整数为 $teams
。我需要做的是生成一个 $teams x $teams
大小介于 1 和 $teams-1
(含)之间的数字矩阵,同时遵守以下规则:
请注意,我们只查看对角线上方的部分。它下面的部分只是它的反射(reflect)(每个数字都是它的反射(reflect) + $teams - 1),对于这个问题来说无关紧要。
前两个条件很容易实现,但第三个条件让我很吃力。我不知道如何实现它,特别是因为 $teams
数字可以是 2 到 20 之间的任何偶数。下面给出了为条件 1 和 2 提供正确输出的代码。有人可以帮我解决第 3 个条件吗?
$teams = 6; //example value - should work for any even Int between 2 and 20
$games = array(); //2D array tracking which week teams will be playing
//do the work
for( $i=1; $i<=$teams; $i++ ) {
$games[$i] = array();
for( $j=1; $j<=$teams; $j++ ) {
$games[$i][$j] = getWeek($i, $j, $teams);
}
}
//show output
echo '<pre>';
$max=0;
foreach($games as $key => $row) {
foreach($row as $k => $col) {
printf('%4d', is_null($col) ? -2 : $col);
if($col > $max){
$max=$col;
}
}
echo "\n";
}
printf("%d teams in %d weeks, %.2f weeks per team\n", $teams, $max, $max/$teams);
echo '</pre>';
function getWeek($home, $away, $num_teams) {
if($home == $away){
return -1;
}
$week = $home+$away-2;
if($week >= $num_teams){
$week = $week-$num_teams+1;
}
if($home>$away){
$week += $num_teams-1;
}
return $week;
}
当前代码(对于 $teams=6)给出以下输出:
-1 1 2 3 4 5
6 -1 3 4 5 1
7 8 -1 5 1 2
8 9 10 -1 2 3
9 10 6 7 -1 4
10 6 7 8 9 -1
6 teams in 10 weeks, 1.67 weeks per team
如您所见,数字 1 同时出现在第 2 列和第 2 行,数字 4 同时出现在第 5 列和第 5 行等,这违反了规则 #3。
最佳答案
这个问题可以在没有任何猜测或回溯的情况下解决,方法是为 n 个团队在 n 轮比赛中创建循环赛程表,然后从中构建一个表示问题中描述的赛程表的数组
要制定时间表,请将 n(此处为 6)个团队排成两行
1 2 3
6 5 4
这是第 1 轮,其中 1 遇到 6,2 遇到 5,3 遇到 4。
然后对于每一轮,轮换除团队 1 之外的团队,给出完整的时间表
Round 1 Round 2 Round 3 Round 4 Round 5
1 2 3 1 3 4 1 4 5 1 5 6 1 6 2
6 5 4 2 6 5 3 2 6 4 3 2 5 4 3
这可以表示为一个数组,每一行代表一个星期,其中第一列中的团队与最后一列中的团队相遇,第二列中的团队与倒数第二个团队相遇,依此类推。
1 2 3 4 5 6 (Week 1: 1-6, 2-5, 3-4)
1 3 4 5 6 2 (Week 2: 1-2, 3-6, 4-5)
1 4 5 6 2 3 (Week 3: 1-3, 2-4, 5-6)
1 5 6 2 3 4 (Week 4: 1-4, 3-5, 2-6)
1 6 2 3 4 5 (Week 5: 1-5, 4-6, 2-3)
将团队表示为行和列,将周作为表条目,这就变成了
-1 1 2 3 4 5
6 -1 4 2 5 3
7 9 -1 5 3 1
8 7 10 -1 1 4
9 10 8 6 -1 2
10 8 6 9 7 -1
以下是为不同数量的团队生成此代码的代码:
<?php
function buildSchedule($teams) {
// Returns a table with one row for each round of the tournament
// Matrix is built by rotating all entries except first one from row to row,
// giving a matrix with zeroes in first column, other values along diagonals
// In each round, team in first column meets team in last,
// team in second column meets second last etc.
$schedule = array();
for($i=1; $i<$teams; $i++){
for($j=0; $j<$teams; $j++){
$schedule[$i][$j] = $j==0 ? 0 : ($i+$j-1) % ($teams-1) + 1;
}
}
return $schedule;
}
function buildWeekTable($schedule) {
// Convert schedule into desired format
//create n x n array of -1
$teams = sizeof($schedule)+1;
$table = array_pad(array(), $teams, array_pad(array(), $teams, -1));
// Set table[i][j] to week where team i will meet team j
foreach($schedule as $week => $player){
for($i = 0; $i < $teams/2 ; $i++){
$team1 = $player[$i];
$team2 = $player[$teams-$i-1];
$table[$team1][$team2] = $team2 > $team1 ? $week : $week + $teams -1;
$table[$team2][$team1] = $team1 > $team2 ? $week : $week + $teams -1;
}
}
return $table;
}
function dumpTable($table){
foreach($table as $row){
$cols = sizeof($row);
for($j=0; $j<$cols; $j++){
printf(" %3d", isset($row[$j]) ? $row[$j] : -1);
}
echo "\n";
}
}
$teams = 6;
$schedule = buildSchedule($teams);
$weekplan = buildWeekTable($schedule);
dumpTable($weekplan);
关于php - 创建行和列中唯一的数字矩阵,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15946255/
我需要将文本放在 中在一个 Div 中,在另一个 Div 中,在另一个 Div 中。所以这是它的样子: #document Change PIN
奇怪的事情发生了。 我有一个基本的 html 代码。 html,头部, body 。(因为我收到了一些反对票,这里是完整的代码) 这是我的CSS: html { backgroun
我正在尝试将 Assets 中的一组图像加载到 UICollectionview 中存在的 ImageView 中,但每当我运行应用程序时它都会显示错误。而且也没有显示图像。 我在ViewDidLoa
我需要根据带参数的 perl 脚本的输出更改一些环境变量。在 tcsh 中,我可以使用别名命令来评估 perl 脚本的输出。 tcsh: alias setsdk 'eval `/localhome/
我使用 Windows 身份验证创建了一个新的 Blazor(服务器端)应用程序,并使用 IIS Express 运行它。它将显示一条消息“Hello Domain\User!”来自右上方的以下 Ra
这是我的方法 void login(Event event);我想知道 Kotlin 中应该如何 最佳答案 在 Kotlin 中通配符运算符是 * 。它指示编译器它是未知的,但一旦知道,就不会有其他类
看下面的代码 for story in book if story.title.length < 140 - var story
我正在尝试用 C 语言学习字符串处理。我写了一个程序,它存储了一些音乐轨道,并帮助用户检查他/她想到的歌曲是否存在于存储的轨道中。这是通过要求用户输入一串字符来完成的。然后程序使用 strstr()
我正在学习 sscanf 并遇到如下格式字符串: sscanf("%[^:]:%[^*=]%*[*=]%n",a,b,&c); 我理解 %[^:] 部分意味着扫描直到遇到 ':' 并将其分配给 a。:
def char_check(x,y): if (str(x) in y or x.find(y) > -1) or (str(y) in x or y.find(x) > -1):
我有一种情况,我想将文本文件中的现有行包含到一个新 block 中。 line 1 line 2 line in block line 3 line 4 应该变成 line 1 line 2 line
我有一个新项目,我正在尝试设置 Django 调试工具栏。首先,我尝试了快速设置,它只涉及将 'debug_toolbar' 添加到我的已安装应用程序列表中。有了这个,当我转到我的根 URL 时,调试
在 Matlab 中,如果我有一个函数 f,例如签名是 f(a,b,c),我可以创建一个只有一个变量 b 的函数,它将使用固定的 a=a1 和 c=c1 调用 f: g = @(b) f(a1, b,
我不明白为什么 ForEach 中的元素之间有多余的垂直间距在 VStack 里面在 ScrollView 里面使用 GeometryReader 时渲染自定义水平分隔线。 Scrol
我想知道,是否有关于何时使用 session 和 cookie 的指南或最佳实践? 什么应该和什么不应该存储在其中?谢谢! 最佳答案 这些文档很好地了解了 session cookie 的安全问题以及
我在 scipy/numpy 中有一个 Nx3 矩阵,我想用它制作一个 3 维条形图,其中 X 轴和 Y 轴由矩阵的第一列和第二列的值、高度确定每个条形的 是矩阵中的第三列,条形的数量由 N 确定。
假设我用两种不同的方式初始化信号量 sem_init(&randomsem,0,1) sem_init(&randomsem,0,0) 现在, sem_wait(&randomsem) 在这两种情况下
我怀疑该值如何存储在“WORD”中,因为 PStr 包含实际输出。? 既然Pstr中存储的是小写到大写的字母,那么在printf中如何将其给出为“WORD”。有人可以吗?解释一下? #include
我有一个 3x3 数组: var my_array = [[0,1,2], [3,4,5], [6,7,8]]; 并想获得它的第一个 2
我意识到您可以使用如下方式轻松检查焦点: var hasFocus = true; $(window).blur(function(){ hasFocus = false; }); $(win
我是一名优秀的程序员,十分优秀!