初始时有 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. 08.C语言:特殊函数

    C语言:特殊函数 1.递归函数: 与普通函数比较,执行过程不同,该函数内部调用它自己,它的执行必须要经过两个阶段:递推阶段,回归阶段: 当不满足回归条件,不再递推: #include <stdi ...

  2. 洛谷 1803 凌乱的yyy

    [题意概述] 给出100W个区间,要求找出尽量多的区间满足它们互补重叠,输出最大的区间数. [题解] 贪心. 对区间按照右端点从小到大排序,然后逐个加入合法区间即可. #include<cstd ...

  3. IE 浏览器证书错误常见问题解答

    有时,你将看到下表的错误消息,比如,此网站的安全证书已经被吊销等等,说明网站的安全证书有问题. 站点的证书允许 Internet Explorer 与站点建立安全连接. SSL证书出现错误有时是证书本 ...

  4. 泛型转换https://www.cnblogs.com/eason-chan/p/3633210.html

    import java.lang.reflect.ParameterizedType;import java.lang.reflect.Type;//总结1.st.getClass==Student. ...

  5. Leetcode 126.单词接龙II

    单词接龙II 给定两个单词(beginWord 和 endWord)和一个字典 wordList,找出所有从 beginWord 到 endWord 的最短转换序列.转换需遵循如下规则: 每次转换只能 ...

  6. chrome源代码目录结构简介(版本4.1.249.1059)

    为了对庞大的源码项目进行分析,先对源码目录树作一个简单的介绍,粗略的了解一下各个模块的功能分布情况,chrome源代码src目录下的结构如下图: app:该目录下的代码主要是和各个操作系统平台相关的应 ...

  7. 20180705关于mysql binlog的解析方式

    来自:https://blog.csdn.net/u012985132/article/details/74964366/ 关系型数据库和Hadoop生态的沟通越来越密集,时效要求也越来越高.本篇就来 ...

  8. Linux下清除DNS缓存

    通常有的时候我们通过域名打不开网页,有可能使DNS缓存的原因(DNS解析的ip地址变了),解决办法如下: 方法一:$nslookup ecafe.pub(这里是你要打开的域名) 方法二:$sudo / ...

  9. 根据View找控制器

    - (UIViewController*)viewController { for (UIView* next = [self superview]; next; next = next.superv ...

  10. sql server internal book

    Frequently Bought Together + + Total price: $131.71 Add all three to CartAdd all three to List Buy t ...