gpt4 book ai didi

php - 夹具 PHP 算法

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:04:04 27 4
gpt4 key购买 nike

与这个锦标赛赛程算法作斗争。

代码运行良好,但我需要帮助将数据插入 mysql我似乎无法访问 $varables..

非常感谢 php 专家的任何调整 ...

$teamnames = "Arsenal|Tottenham|Leeds|Man United|Liverpool";



# XXX check for int
print show_fixtures(isset($_GET['teams']) ? nums(intval($_GET['teams'])) : explode("|", trim($teamnames)));


function nums($n) {
$ns = array();
for ($i = 1; $i <= $n; $i++) {
$ns[] = $i;
}
return $ns;
}

function show_fixtures($names) {
$teams = sizeof($names);

print "<p>Fixtures for $teams teams.</p>";

// If odd number of teams add a "ghost".
$ghost = false;
if ($teams % 2 == 1) {
$teams++;
$ghost = true;
}

// Generate the fixtures using the cyclic algorithm.
$totalRounds = $teams - 1;
$matchesPerRound = $teams / 2;
$rounds = array();
for ($i = 0; $i < $totalRounds; $i++) {
$rounds[$i] = array();
}

for ($round = 0; $round < $totalRounds; $round++) {
for ($match = 0; $match < $matchesPerRound; $match++) {
$home = ($round + $match) % ($teams - 1);
$away = ($teams - 1 - $match + $round) % ($teams - 1);
// Last team stays in the same place while the others
// rotate around it.
if ($match == 0) {
$away = $teams - 1;
}
$rounds[$round][$match] = team_name($home + 1, $names)
. " v " . team_name($away + 1, $names);


}
}

// Interleave so that home and away games are fairly evenly dispersed.
$interleaved = array();
for ($i = 0; $i < $totalRounds; $i++) {
$interleaved[$i] = array();
}

$evn = 0;
$odd = ($teams / 2);
for ($i = 0; $i < sizeof($rounds); $i++) {
if ($i % 2 == 0) {
$interleaved[$i] = $rounds[$evn++];
} else {
$interleaved[$i] = $rounds[$odd++];
}
}

$rounds = $interleaved;

// Last team can't be away for every game so flip them
// to home on odd rounds.
for ($round = 0; $round < sizeof($rounds); $round++) {
if ($round % 2 == 1) {
$rounds[$round][0] = flip($rounds[$round][0]);
}
}

// Display the fixtures
for ($i = 0; $i < sizeof($rounds); $i++) {
print "<hr><p>Round " . ($i + 1) . "</p>\n";
foreach ($rounds[$i] as $r) {


print $r . "<br />";


}
print "<br />";
}
print "<hr>Second half is mirror of first half";
$round_counter = sizeof($rounds) + 1;
for ($i = sizeof($rounds) - 1; $i >= 0; $i--) {
print "<hr><p>Round " . $round_counter . "</p>\n";
$round_counter += 1;
foreach ($rounds[$i] as $r) {
print flip($r) . "<br />";
}
print "<br />";
}
print "<br />";

if ($ghost) {
print "Matches against team " . $teams . " are byes.";
}
}

function flip($match) {
$components = split(' v ', $match);


return "$components[1]" . " v " . "$components[0]";

}

function team_name($num, $names) {
$i = $num - 1;
if (sizeof($names) > $i && strlen(trim($names[$i])) > 0) {
return trim($names[$i]);
} else {
return "BYE";
}
}

最佳答案

我不完全确定你挂断了什么(你的问题真的应该更具体,如 FAQ 所指定),但我怀疑这是一个范围问题。

当您在函数内设置变量时,该变量只能在该函数内访问。例如:

function do_something() {
$a = 'something!';
}
do_something();
echo $a;

这将导致 PHP 通知您 PHP 不知道什么 $a在它试图echo的范围内.现在,如果我修改这个脚本...

$a = '';
function do_something() {
global $a; // Lets PHP know we want to use $a from the global scope
$a = 'something!';
}
do_something();
echo $a;

这会起作用并输出“something!”,因为$a在函数之外的范围内被“定义”。

您可以在 PHP 文档中阅读有关变量范围的更多信息:http://php.net/manual/en/language.variables.scope.php

现在,您需要注意的另一件事是输出用户数据。在您的脚本中,您直接从 $_GET 获取数据并将其打印到页面上。为什么这样不好?有人可以将一些不错的 JavaScript 注入(inject)您的页面(或他们想要的任何东西)并窃取用户的 session 。你应该使用 htmlspecialchars() 任何时候你需要输出一个变量。即使它只是一个团队名称,你也永远不知道某个团队什么时候会贴上一个;。或 <>或其中的一些其他保留字符。

最后,我强烈建议不要将逻辑与计算混为一谈。让您的程序解决所有问题,然后循环遍历输出数据。您应该能够将此类问题的全部数据保存在一个漂亮的关联数组或您想出的一些狡猾的对象中。

关于php - 夹具 PHP 算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9058284/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com