初始时有 n 个灯泡关闭。 第 1 轮,你打开所有的灯泡。 第 2 轮,每两个灯泡切换一次开关。 第 3 轮,每三个灯泡切换一次开关(如果关闭,则打开,如果打开则关闭)。对于第 i 轮,你每 i 个灯泡切换一次开关。 对于第 n 轮,你只切换最后一个灯泡的开关。 找出 n 轮后有多少个亮着的灯泡。
示例:
给定 n = 3。状态off表示灯泡关闭,on表示开启。
初始时, 灯泡状态 [off, off, off].
第一轮后, 灯泡状态 [on, on, on].
第二轮后, 灯泡状态 [on, off, on].
第三轮后, 灯泡状态 [on, off, off].
你应该返回1,因为只有一个灯泡还亮着。
详见:https://leetcode.com/problems/bulb-switcher/description/
C++:

class Solution {
public:
int bulbSwitch(int n) {
int res=1;
while(res*res<=n)
{
++res;
}
return res-1;
}
};

参考:http://www.cnblogs.com/grandyang/p/5100098.html

319 Bulb Switcher 灯泡开关的更多相关文章

  1. [LeetCode]319. Bulb Switcher灯泡开关

    智商压制的一道题 这个题有个数学定理: 一般数(非完全平方数)的因子有偶数个 完全平凡数的因子有奇数个 开开关的时候,第i个灯每到它的因子一轮的时候就会拨动一下,也就是每个灯拨动的次数是它的因子数 而 ...

  2. [LeetCode] Bulb Switcher 灯泡开关

    There are n bulbs that are initially off. You first turn on all the bulbs. Then, you turn off every ...

  3. Leetcode319. Bulb Switcher灯泡开关

    初始时有 n 个灯泡关闭. 第 1 轮,你打开所有的灯泡. 第 2 轮,每两个灯泡你关闭一次. 第 3 轮,每三个灯泡切换一次开关(如果关闭则开启,如果开启则关闭).第 i 轮,每 i 个灯泡切换一次 ...

  4. LeetCode 319 ——Bulb Switcher——————【数学技巧】

    319. Bulb Switcher My Submissions QuestionEditorial Solution Total Accepted: 15915 Total Submissions ...

  5. 【LeetCode】319. Bulb Switcher 解题报告(Python)

    [LeetCode]319. Bulb Switcher 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/bulb ...

  6. 319. Bulb Switcher

    题目: There are n bulbs that are initially off. You first turn on all the bulbs. Then, you turn off ev ...

  7. 319. Bulb Switcher——本质:迭代观察,然后找规律

    There are n bulbs that are initially off. You first turn on all the bulbs. Then, you turn off every ...

  8. Java [Leetcode 319]Bulb Switcher

    题目描述: There are n bulbs that are initially off. You first turn on all the bulbs. Then, you turn off ...

  9. LeetCode 319. Bulb Switcher

    There are n bulbs that are initially off. You first turn on all the bulbs. Then, you turn off every ...

随机推荐

  1. calculate Cp history (from Fluent) using Matlab

    input data : unscaled time history of moment/thrust from ANSYS fluent example of input data, "m ...

  2. sql学习笔记:表的运算

    在MICK的<SQL基础教程>里读到的一章,写的很好,之前很乱的思路变清晰了很多.简单来说,表的运算主要是两种:列的运算和行的运算. 表的加减法 这里是对表的列操作(向下扩展).因此,按照 ...

  3. 51NOD 1277 字符串中的最大值(KMP)

    >>点击进入原题测试<< 思路:用KMP优化的暴力写了一遍,超时!没有充分利用KMP中next数组的性质. 首先这个题是肯定要用到KMP算法的,然后会有一个next[]数组. ...

  4. POJ 2065 高斯消元求解问题

    题目大意: f[k] = ∑a[i]*k^i % p 每一个f[k]的值就是字符串上第 k 个元素映射的值,*代表f[k] = 0 , 字母代表f[k] = str[i]-'a'+1 把每一个k^i求 ...

  5. 一些非常有用的工具类之javamail(from韩顺平)

    之前编写一个类淘宝服务器时,需要使用javamail发送邮件,搜到的一个工具类,很有用. 需要下载导入:activation.jar和mail.jar package com.cx.service; ...

  6. 【Java基础】基本类型与运算【重要】

    0.   Java基本数据类型 Java的位运算(bitwise operators)直接对整数类型的位进行操作,这些整数类型包括long.int.short.char和 byte,位运算符具体如下表 ...

  7. spring boot jpa 事务管理

    spring boot 对jpa的支持极为方便,基本上不需要作太多配置,只需要加上注解就能支持事务: @Controller @Transactional(rollbackOn = Exception ...

  8. MySQL慢日志切割邮件发送脚本

    #!/bin/bashtime=`date -d yesterday +"%Y-%m-%d"`slowlog='/usr/local/percona/data/slow.log'# ...

  9. VM Workstation 虚拟机下如何安装VMtools

    不同版本Linux原理类似,这里以Debian为例. 1 根据提示点击Install Tools,CDROM中出现VmwareTools的安装包,在你的主文件夹下新建一个文件夹(随便叫什么都行,我新建 ...

  10. jvm 堆内存 栈内存 大小设置

                        4种方式配置不同作用域的jvm的堆栈内存. 1.Eclise 中设置jvm内存: 改动eclipse的配置文件,对全部project都起作用 改动eclipse ...