gpt4 book ai didi

PHP Pascal 三 Angular 形在表格中显示不正确

转载 作者:太空宇宙 更新时间:2023-11-04 07:59:38 24 4
gpt4 key购买 nike


我一直在寻找几个小时,但我没有找到任何解决方案。当我想在表格中显示我的 Pascal 三 Angular 形时遇到问题,但它实际上没有正确显示。这是代码:`

<!DOCTYPE html>
<html>
<head>
<title>Triangle de Pascal</title>
<meta charset="utf-8">
</head>
<body>
<form action="index.php" method="POST">
<label for="depth">Profondeur : </label>
<input type="number" id="depth" name="depth" min="1" max="100">
<input type="submit">
</form>
<style>
table{
border-collapse: collapse;
}
td{
text-align: center;
padding: 0px 5px; border: 1px solid black;
box-sizing: border-box;
}
</style>
<table>
<?php
$depth = $_POST['depth'];
$length = $depth;
$array[0][0] = 1;
for ($i = 1; $i < $depth; $i++){
$array[$i][0] = 1;
echo "<tr>";
for($j = 1; $j <= $i; $j++){
if($j == $i){
$array[$i][$j] = 1;
}
else{
$array[$i][$j] = $array[$i-1][$j-1] + $array[$i-1][$j];
}
echo "<td colspan=".floor($length * $length / ($i)).'>'.$array[$i][$j]."</td>";
}
echo "</tr>";
}
?>
</table>
</body>
</html>

如果你能帮我解决那个问题,那就太好了,我真的不知道问题出在哪里

最佳答案

你的循环中有一个逻辑错误,当 $j==1 你应该输出 1。你的代码的 PHP 部分应该是这样的(我也删除了一些无用的初始化):

<?php
$depth = $_POST['depth'];
$length = $depth;
for ($i = 1; $i < $depth; $i++){
echo "<tr>";
for($j = 1; $j <= $i; $j++){
if($j == 1 || $j == $i){
$array[$i][$j] = 1;
}
else{
$array[$i][$j] = $array[$i-1][$j-1] + $array[$i-1][$j];
}
echo "<td colspan=".floor($length * $length / ($i)).'>'.$array[$i][$j]."</td>";
}
echo "</tr>";
}
?>

这是一个在您的输入框中将 15 作为值的示例:

enter image description here

关于PHP Pascal 三 Angular 形在表格中显示不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47095205/

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