php - 计算 < X 的幂的 PHP 脚本
问题描述
我的 PHP 脚本用于计算功率时出现问题。
$A = $_POST["a"]; //Base number
$B = $_POST["b"]; //Value that POW of $A can't exceed. for example if A = 2, B = 1000, then highest solution = 512
for ($i = 1; $A <= $B; $i++) {
echo $i . ". ";
echo pow($A, $i);
echo '<br>';
}
通过上面提到的示例,我知道这个脚本仍然会显示 1024,但问题是它根本不起作用。我尝试了很多其他方法,但对我没有任何作用......请任何指导都非常感谢。
解决方案
让我们举个例子,$A = 2; $B = 10;
所以你期望得到 2、4、8。
您的 for 循环具有 as 条件,只要$A <= $B
.
让我们手动进行一些迭代:
运行 1:$A: 2, $B: 10, ($A <= $B): true, $i: 1, output: "1. 2<br>"
运行 2:$A: 2, $B: 10, ($A <= $B): true, $i: 2, output: "1. 4<br>"
运行 3:$A: 2, $B: 10, ($A <= $B): true, $i: 3, output: "1. 8<br>"
运行 4:$A: 2, $B: 10, ($A <= $B): true, $i: 4, output: "1. 16<br>"
运行 5:$A: 2, $B: 10, ($A <= $B): true, $i: 5, output: "1. 32<br>"
运行 X:$A: 2, $B: 10, ($A <= $B): true, $i: X, output: "1. 2^X<br>"
正如我们所见,测试条件$A <= $B
始终为真。如何解决这个问题?一种选择是更新 variable $A
,如下所示:
$A = $_POST["a"]; //Base number
$B = $_POST["b"]; //Value that POW of $A can't exceed. for example if A = 2, B = 1000, then highest solution = 512
for ($i = 1; $A <= $B; $i++) {
$A = pow($A, $i);
if ($A <= $B) {
echo $i . ". ";
echo $A;
echo '<br>';
}
}
另一种选择是使用中断条件:
$A = $_POST["a"]; //Base number
$B = $_POST["b"]; //Value that POW of $A can't exceed. for example if A = 2, B = 1000, then highest solution = 512
for ($i = 1; true; $i++) {
$A = pow($A, $i);
if ($A > $B) {
break;
}
echo $i . ". ";
echo $A;
echo '<br>';
}
这是一个无限循环,通常写成
$A = $_POST["a"]; //Base number
$B = $_POST["b"]; //Value that POW of $A can't exceed. for example if A = 2, B = 1000, then highest solution = 512
$i = 1;
while (true) {
if ($A > $B) {
break;
}
echo $i . ". ";
$A = pow($A, $i);
echo $A;
echo '<br>';
$i++;
}
最后一件事,因为$A
并由$B
客户提交,您不应该假设它们是数字。您可以使用以下方法简单地将它们转换为数字:
$A = (int) $_POST['a'];
$B = (int) $_POST['b'];
我个人将如何编写该代码:
// get variables
$base = (int) $_POST["a"];
$max = (int) $_POST["b"];
// validate input
if ($max < $base) {
echo 'error: Max number bigger than base number!<br>';
} elseif ($base < 2) {
echo 'error: Base number needs to be >= 2!<br>';
}
/** SOLUTION 1 - using `pow` */
// starting exponent
$exp = 1;
// assign $base^$exp to $result and test it against $max
while (($result = pow($base, $exp)) <= $max) {
// echo current row
echo $exp.'. '.$result.'<br>';
// increment exponent
$exp++;
}
/** SOLUTION 2 - using dynamic programming instead of calling `pow` in each iteration */
// multiply $result with the base and check against $max
for ($result = 1, $exp = 1; ($result *= $base) <= $max; $exp++) {
echo $exp.'. '.$result.'<br>';
}
/** In case you don't need the exponent:
for ($result = 1; ($result *= $base) <= $max;) {
echo $result.'<br>';
}
*/
尝试选择解释他们正在做什么的变量,并添加评论来解释正在发生的事情。
推荐阅读
- python - Get row values as list when column value equal something
- r - 打印出巨大的情节,这样我就可以看到结果
- python - 有没有办法将标签和输入框都集中在 tkinter 的特定行上?
- spring - Spring Boot java.lang.IllegalArgumentException:不是托管类型
- pip - pipenv 模拟 pip -f 选项的选项
- rust - 如何在wasm32中获取rust struct关联函数、struct方法或trait方法导出函数
- haskell - Haskell 中 Double 的有界实例
- python - 使用正则表达式在文本文件中用另一个相同大小的数字替换一个数字
- php - 如何使用 php 在网站上提供来自 mysql 数据库的信息
- mysql - 计算多列中的匹配项