面试题一,面试题
面试题一,面试题
最近在网上看见一道面试题,随手做了一下,也作为我开始写博客的第一篇开始。
因为我最近在学习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> ?>
有更好的方法,会后续更新。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



How to set up keyboard startup on Gigabyte's motherboard. First, if it needs to support keyboard startup, it must be a PS2 keyboard! ! The setting steps are as follows: Step 1: Press Del or F2 to enter the BIOS after booting, and go to the Advanced (Advanced) mode of the BIOS. Ordinary motherboards enter the EZ (Easy) mode of the motherboard by default. You need to press F7 to switch to the Advanced mode. ROG series motherboards enter the BIOS by default. Advanced mode (we use Simplified Chinese to demonstrate) Step 2: Select to - [Advanced] - [Advanced Power Management (APM)] Step 3: Find the option [Wake up by PS2 keyboard] Step 4: This option The default is Disabled. After pulling down, you can see three different setting options, namely press [space bar] to turn on the computer, press group

1. Processor When choosing a computer configuration, the processor is one of the most important components. For playing games like CS, the performance of the processor directly affects the smoothness and response speed of the game. It is recommended to choose Intel Core i5 or i7 series processors because they have powerful multi-core processing capabilities and high frequencies, and can easily cope with the high requirements of CS. 2. Graphics card Graphics card is one of the important factors in game performance. For shooting games such as CS, the performance of the graphics card directly affects the clarity and smoothness of the game screen. It is recommended to choose NVIDIA GeForce GTX series or AMD Radeon RX series graphics cards. They have excellent graphics processing capabilities and high frame rate output, and can provide a better gaming experience. 3. Memory power

Starting from scratch, I will teach you step by step how to install Flask and quickly build a personal blog. As a person who likes writing, it is very important to have a personal blog. As a lightweight Python Web framework, Flask can help us quickly build a simple and fully functional personal blog. In this article, I will start from scratch and teach you step by step how to install Flask and quickly build a personal blog. Step 1: Install Python and pip Before starting, we need to install Python and pi first

Blog, also translated as web log, blog or blog, is a website that is usually managed by individuals and posts new articles from time to time. So how to set up a blog? What are the PHP blog systems? Which blogging system is best to use? Below, PHP Chinese website will summarize and share the top ten open source PHP blog systems with you. Let’s take a look!

SPDIFOUT connection line sequence on the motherboard. Recently, I encountered a problem regarding the wiring sequence of the wires. I checked online. Some information says that 1, 2, and 4 correspond to out, +5V, and ground; while other information says that 1, 2, and 4 correspond to out, ground, and +5V. The best way is to check your motherboard manual. If you can't find the manual, you can use a multimeter to measure it. Find the ground first, then you can determine the order of the rest of the wiring. How to connect motherboard VDG wiring When connecting the VDG wiring of the motherboard, you need to plug one end of the VGA cable into the VGA interface of the monitor and the other end into the VGA interface of the computer's graphics card. Please be careful not to plug it into the motherboard's VGA port. Once connected, you can

With the development of the Internet, blogs have become a platform for more and more people to share their lives, knowledge and ideas. If you also want to create a blog of your own, then this article will introduce how to use PHP and SQLite to create a simple blog. Determine the needs Before starting to create a blog, we need to determine the functions we want to achieve. For example: Create a blog post Edit a blog post Delete a blog post Display a list of blog posts Display blog post details User authentication and permission control Install PHP and SQLite We need to install PHP and S

Glodon Software is a software company focusing on the field of building informatization. Its products are widely used in all aspects of architectural design, construction, and operation. Due to the complex functions and large data volume of Glodon software, it requires high computer configuration. This article will elaborate on the computer configuration recommendations of Glodon Software from many aspects to help readers choose a suitable computer configuration processor. Glodon Software requires a large amount of data calculation and processing when performing architectural design, simulation and other operations. Therefore, the requirements for the processor are higher. It is recommended to choose a multi-core, high-frequency processor, such as Intel i7 series or AMD Ryzen series. These processors have strong computing power and multi-thread processing capabilities, and can better meet the needs of Glodon software. Memory Memory is affecting computing

How to solve the problem of jQueryAJAX error 403? When developing web applications, jQuery is often used to send asynchronous requests. However, sometimes you may encounter error code 403 when using jQueryAJAX, indicating that access is forbidden by the server. This is usually caused by server-side security settings, but there are ways to work around it. This article will introduce how to solve the problem of jQueryAJAX error 403 and provide specific code examples. 1. to make
