Leetcode 之 Bulb Switcher - 灯泡开关

看懂题目花了很长时间:

题目是这样的:

英文:There are n bulbs that are initially off. You first turn on all the bulbs. Then, you turn off every second bulb. On the third round, you toggle every third bulb (turning on if it‘s off or turning off if it‘s on). For the nth round, you only toggle the last bulb. Find how many bulbs are on after n rounds.

中文:现有n个灯泡,默认都是关闭的。第一轮会打开所有的灯泡,第二轮关闭所有偶数次序的灯泡,第三轮翻转所有次序为三的倍数位置的灯泡,直到第n轮拨动最后一个灯泡的开关。试确定第n轮后还有几盏灯是亮的。

要求出哪些灯是关闭的,比如第十盏灯,有2,和5,1和10,分别发生开和关,开关偶数次,那么肯定是关掉的,也就是说只有平方数才是亮的,也就是找出平方数的个数就可以了。

class Solution {
public:
    int bulbSwitch(int n) {
        int i ;
        for(i = 0 ; i < n ;i++)
            {
                if(((i+1)*(i+1))>n)
                   break;
            }
        return i;
    }
};
时间: 2024-08-28 10:03:20

Leetcode 之 Bulb Switcher - 灯泡开关的相关文章

319 Bulb Switcher 灯泡开关

初始时有 n 个灯泡关闭. 第 1 轮,你打开所有的灯泡. 第 2 轮,每两个灯泡切换一次开关. 第 3 轮,每三个灯泡切换一次开关(如果关闭,则打开,如果打开则关闭).对于第 i 轮,你每 i 个灯泡切换一次开关. 对于第 n 轮,你只切换最后一个灯泡的开关. 找出 n 轮后有多少个亮着的灯泡.示例:给定 n = 3.状态off表示灯泡关闭,on表示开启.初始时, 灯泡状态 [off, off, off].第一轮后, 灯泡状态 [on, on, on].第二轮后, 灯泡状态 [on, off,

[LeetCode][JavaScript]Bulb Switcher

Bulb Switcher There are n bulbs that are initially off. You first turn on all the bulbs. Then, you turn off every second bulb. On the third round, you toggle every third bulb (turning on if it's off or turning off if it's on). For the nth round, you

Leetcode 319. Bulb Switcher

这是一个非常巧妙的题目,网站称之为brain teaser,意思就是代码写起来会非常简介(就是逗你玩:)),题目如下: There are n bulbs that are initially off. You first turn on all the bulbs. Then, you turn off every second bulb. On the third round, you toggle every third bulb (turning on if it's off or tu

Leetcode 319 Bulb Switcher 找规律

有n盏关着的灯,第k轮把序号为k倍数的关着的灯打开,开着的灯关闭. class Solution { public: int bulbSwitch(int n) { return (int)sqrt(n*1.0); } };

Leetcode 672.灯泡开关II

灯泡开关II 现有一个房间,墙上挂有 n 只已经打开的灯泡和 4 个按钮.在进行了 m 次未知操作后,你需要返回这 n 只灯泡可能有多少种不同的状态. 假设这 n 只灯泡被编号为 [1, 2, 3 ..., n],这 4 个按钮的功能如下: 将所有灯泡的状态反转(即开变为关,关变为开) 将编号为偶数的灯泡的状态反转 将编号为奇数的灯泡的状态反转 将编号为 3k+1 的灯泡的状态反转(k = 0, 1, 2, ...) 示例 1: 输入: n = 1, m = 1. 输出: 2 说明: 状态为:

灯泡开关案例

<html> <head><title>灯泡开关案例</title> <!--其实灯泡开关的核心就仅是两张图片替换 --> <script> function xx() { var a = window.document.getElementById('test'); if(a.src.indexOf('on')>=0)//查找a里面src属性里是否有on这个字符.如果有就会得到字符所在的位置,范围值为数字.没找到就等于-1 {

灯泡开关问题

有编号1~100个灯泡,起初所有的灯都是灭的.有100个同学来按灯泡开关,如果灯是亮的,那么按过开关之后,灯会灭掉.如果灯是灭的,按过开关之后灯会亮.现在开始按开关.第1个同学,把所有的灯泡开关都按一次(按开关灯的编号: 1,2,3,......100).第2个同学,隔一个灯按一次(按开关灯的编号: 2,4,6,......,100).第3个同学,隔两个灯按一次(按开关灯的编号: 3,6,9,......,99).......问题是,在第100个同学按过之后,有多少盏灯是亮着的?这些灯的编号是多

【LeetCode从零单刷】Bulb Switcher

题目: There are n bulbs that are initially off. You first turn on all the bulbs. Then, you turn off every second bulb. On the third round, you toggle every third bulb (turning on if it's off or turning off if it's on). For the nth round, you only toggl

Leetcode Bulb Switcher

There are n bulbs that are initially off. You first turn on all the bulbs. Then, you turn off every second bulb. On the third round, you toggle every third bulb (turning on if it's off or turning off if it's on). For the nth round, you only toggle th