使用一个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

查看:298
本文介绍了使用一个for循环输出1 2 4 8 11 12 14 18 ...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何生成数字序列,如

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语句如何工作?

  • 调用arrayBuilder()时,必须发送$max值(表示返回数组中可能的最高值),并且可以选择指定$num(返回数组中的第一个数字),否则默认为值是1.
  • arrayBuilder()内,$array被声明为空数组.如果用户的输入值不允许在for循环中进行单个迭代,则这一点很重要.这行代码对于良好的编码习惯至关重要,可确保在任何情况下都不会发生通知/警告/错误.
  • for循环是php中最复杂的循环(因此手册)及其三个表达式是打包我使用的技术的理想方法.
    • When you call 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.
    • Inside 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.
    • A 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.
      • The first expression $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.
      • The second expression is the only tricky/magical aspect of my entire code block. This expression is called 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.

        这篇关于使用一个for循环输出1 2 4 8 11 12 14 18 ...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆