最近在网上看见一道面试题,随手做了一下,也作为我开始写博客的第一篇开始。
因为我最近在学习PHP,所以我就用PHP来解决它,不过天下语言是一家,其他的语言也可参照思想。题目比较简单,如下:
大厅里有100盏灯,每盏灯都编了号码,分别为1-100。每盏灯由一个开关来控制。(开关按一下,灯亮,再按一下灯灭。开关的编号与被控制的灯相同。)开始时,灯是全灭的。现在按照以下规则按动开关。
第一次,将所有的灯点亮。
第二次,将所有2的倍数的开关按一下。
第三次,将所有3的倍数的开关按一下。
以此类推。第N次,将所有N的倍数的开关按一下。
问第100次按完以后,大厅里还有几盏灯是亮的。
方法如 下:
<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> ?>
有更好的方法,会后续更新。