使用一个for循环输出1 2 4 8 11 12 14 18 ... [英] Make an output of 1 2 4 8 11 12 14 18... using one for loop
问题描述
如何生成数字序列,如
1 2 4 8 11 12 14 18 ...
1 2 4 8 11 12 14 18 ...
(每4个数字加10),并具有以下附加要求:
(plus 10 every 4 numbers) with the following additional requirements:
- 仅使用一个循环
- 当序列中的值大于指定的输入时,输出应停止
$input = 24;
1 2 4 8 11 12 14 18 21 22 24
1 2 4 8 11 12 14 18 21 22 24
$input = 20;
1 2 4 8 11 12 14 18
1 2 4 8 11 12 14 18
这是我到目前为止尝试过的:
Here's what I tried so far:
<?php
// sample user input
$input = 20;
$number = 1;
$counter = 0;
$array = array();
//conditions
while ($counter < 4) {
$counter++;
array_push($array, $number);
$number += $number;
}
//outputs
for ($x = 0; $x < count($array); $x++) {
echo $array[$x];
echo " ";
}
推荐答案
代码:(
此方法与localheinz的答案非常相似,但使用的是 beetlejuice 向我介绍的技术,该技术速度更快且php版本安全.我只是在发布前阅读了localheinz的答案;这是几乎相同的知识融合的问题.我只是用我能想到的最好的方法来满足这个简短要求. This method is very similar to localheinz's answer, but uses a technique introduced to me by beetlejuice which is faster and php version safe. I only read localheinz's answer just before posting; this is a matter of nearly identical intellectual convergence. I am merely satisfying the brief with the best methods that I can think of. 如何/为什么不使用查找数组或if语句如何工作? 这篇关于使用一个for循环输出1 2 4 8 11 12 14 18 ...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
arrayBuilder()
时,必须发送$max
值(表示返回数组中可能的最高值),并且可以选择指定$num
(返回数组中的第一个数字),否则默认为值是1
.arrayBuilder()
内,$array
被声明为空数组.如果用户的输入值不允许在for
循环中进行单个迭代,则这一点很重要.这行代码对于良好的编码习惯至关重要,可确保在任何情况下都不会发生通知/警告/错误.for
循环是php中最复杂的循环(因此手册)及其三个表达式是打包我使用的技术的理想方法.
$i=0;
是php开发人员经常看到的东西.这是一次$i
等于0
的一次性声明,仅在before
第一次迭代中出现.before
.我将尝试对其进行分解:(括号对于该表达式至关重要,以避免由于按位向左移"运算符,对于每个余数",将"$num
乘以2
".这是实现[don't double],[double once],[double twice],[double three times]
的4位数模式创建的非常快速的方法:1,2,4,8
,2,4,8,16
等. bitwise operators
总是比arithmetic operators
更有效.
使用算术运算符取模可确保预期的内核数模式每四次重复重复一次.+
添加(如果有任何混淆,请勿串联)10*floor($i/4)
向下舍入$i
除以4
然后乘以10
,以便前四个迭代获得加分0
,接下来的四个迭代获得10
,接下来的四个迭代获得)
右括号包含比较运算符的左侧<=$max
允许迭代直到超过$max
值.
arrayBuilder()
, you must send a $max
value (representing the highest possible value in the returned array) and optionally, you can nominate $num
(the first number in the returned array) otherwise the default value is 1
.arrayBuilder()
, $array
is declared as an empty array. This is important if the user's input value(s) do not permit a single iteration in the for
loop. This line of code is essential for good coding practices to ensure that under no circumstances should a Notice/Warning/Error occur.for
loop is the most complex loop in php (so says the manual), and its three expressions are the perfect way to package the techniques that I use.
$i=0;
is something that php developers see all of the time. It is a one-time declaration of $i
equalling 0
which only occurs before
the first iteration.before
every iteration. I'll try to break it down: (parentheses are vital to this expression to avoid unintended results due to operator precedence
(
open parenthesis to contain leftside of comparison operator$val=
declare $val
for use inside loop on each iteration($num<<$i%4)
because of precedence this is the same as $num<<($i%4)
meaning: "find the remainder of $i
divided by 4 then use the bitwise "shift left" operator to "multiply $num
by 2
for every "remainder". This is a very fast way of achieving the 4-number pattern of [don't double],[double once],[double twice],[double three times]
to create: 1,2,4,8
, 2,4,8,16
, and so on. bitwise operators
are always more efficient than arithmetic operators
.
The use of the arithmetic operator modulo ensure that the intended core number pattern repeats every four iterations.+
add (not concatenation in case there is any confusion)10*floor($i/4)
round down $i
divided by 4
then multiply by 10
so that the first four iterations get a bonus of 0
, the next four get 10
, the next four get 20
, and so on.)
closing parenthesis to contain leftside of comparison operator<=$max
allow iteration until the $max
value is exceeded.