Home > Backend Development > PHP Tutorial > Interview Question 1, Interview Questions_PHP Tutorial

Interview Question 1, Interview Questions_PHP Tutorial

WBOY
Release: 2016-07-13 09:44:07
Original
783 people have browsed it

Interview Question 1, Interview Question

I recently saw an interview question on the Internet, and I did it casually. It also served as the first post for me to start blogging.

Because I am learning PHP recently, I will use PHP to solve it. However, the world's languages ​​are the same, and other languages ​​​​can also refer to the idea. The questions are relatively simple, as follows:

There are 100 lights in the hall, and each light is numbered, ranging from 1-100. Each light is controlled by a switch. (Press the switch once to turn the light on, and press it again to turn off the light. The number of the switch is the same as the controlled light.) At the beginning, all the lights are off. Now press the switch according to the following rules.
For the first time, light all the lights.
The second time, press all the switches that are multiples of 2.
For the third time, press all the switches that are multiples of 3.
 And so on. For the Nth time, press all switches that are multiples of N.
Ask how many lights are still on in the hall after pressing the button for the 100th time.

The method is as follows:

<span> 1</span> <?<span>php
</span><span> 2</span> 
<span> 3</span>     <span>//</span><span>totle:总共的灯盏数,times:按动开关的次数</span>
<span> 4</span>     <span>function</span> switchLight(<span>$totle</span>,<span>$times</span><span>){
</span><span> 5</span>         <span>//</span><span>定义开关打开和关闭的状态属性</span>
<span> 6</span>             <span>$on</span>=1<span>;
</span><span> 7</span>             <span>$off</span>=-1<span>;
</span><span> 8</span>         <span>//</span><span>定义一个按动开关的动作</span>
<span> 9</span>             <span>$oc</span>=-1<span>;
</span><span>10</span>         <span>//</span><span>建立一个数组,动态存储灯盏数目,并初始化状态</span>
<span>11</span>             <span>$lights</span>=<span>array</span><span>();
</span><span>12</span>             <span>for</span>(<span>$i</span>=1;<span>$i</span><=<span>$totle</span>;<span>$i</span>++<span>){
</span><span>13</span>                 <span>$lights</span>[<span>$i</span>]=<span>$off</span><span>;
</span><span>14</span> <span>            }
</span><span>15</span>         <span>//</span><span>判断没有按动开关的情况</span>
<span>16</span>         <span>if</span>(<span>$times</span>==0<span>){
</span><span>17</span>             <span>return</span> 0<span>;
</span><span>18</span> <span>        }
</span><span>19</span>         <span>//</span><span>循环判断,并按动开关</span>
<span>20</span>         <span>for</span>(<span>$j</span>=1;<span>$j</span><=<span>$times</span>;<span>$j</span>++<span>){
</span><span>21</span>             <span>for</span>(<span>$k</span>=1;<span>$k</span><=<span>$totle</span>;<span>$k</span>++<span>){
</span><span>22</span>                 <span>if</span>(!(<span>$k</span>%<span>$j</span><span>))
</span><span>23</span>                     <span>$lights</span>[<span>$k</span>]*=<span>$oc</span><span>;
</span><span>24</span> <span>            }    
</span><span>25</span> <span>        }
</span><span>26</span>         <span>//</span><span>遍历出所有为on的灯,并存入新数组</span>
<span>27</span>         <span>$newLights</span>=<span>array</span><span>();
</span><span>28</span>         <span>for</span>(<span>$r</span>=1;<span>$r</span><=<span>$totle</span>;<span>$r</span>++<span>){
</span><span>29</span>             <span>if</span>(<span>$lights</span>[<span>$r</span>]==<span>$on</span><span>){
</span><span>30</span>                 <span>$newLights</span>[]=<span>$r</span><span>;
</span><span>31</span> <span>            }
</span><span>32</span> <span>        }
</span><span>33</span>         <span>return</span> <span>$newLights</span><span>;
</span><span>34</span>          
<span>35</span> <span>    }
</span><span>36</span>     
<span>37</span>     <span>$newLights</span>=switchLight(100,100<span>);
</span><span>38</span>     <span>echo</span> "结果有".<span>count</span>(<span>$newLights</span>)."盏灯亮,具体如下:"<span>;
</span><span>39</span>     <span>foreach</span>(<span>$newLights</span> <span>as</span> <span>$light</span><span>){
</span><span>40</span>         <span>echo</span> <span>$light</span><span>;
</span><span>41</span>         <span>echo</span> "\n"<span>;
</span><span>42</span> <span>    }
</span><span>43</span> ?>
Copy after login

There is a better method and will be updated later.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1051221.htmlTechArticleInterview question one, interview question I recently saw an interview question on the Internet, and I did it casually, and I started writing it. The first blog post begins. Because I am learning PHP recently, I use P...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template