319 Bulb Switcher 灯泡开关
初始时有 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 灯泡开关的更多相关文章
- [LeetCode]319. Bulb Switcher灯泡开关
智商压制的一道题 这个题有个数学定理: 一般数(非完全平方数)的因子有偶数个 完全平凡数的因子有奇数个 开开关的时候,第i个灯每到它的因子一轮的时候就会拨动一下,也就是每个灯拨动的次数是它的因子数 而 ...
- [LeetCode] Bulb Switcher 灯泡开关
There are n bulbs that are initially off. You first turn on all the bulbs. Then, you turn off every ...
- Leetcode319. Bulb Switcher灯泡开关
初始时有 n 个灯泡关闭. 第 1 轮,你打开所有的灯泡. 第 2 轮,每两个灯泡你关闭一次. 第 3 轮,每三个灯泡切换一次开关(如果关闭则开启,如果开启则关闭).第 i 轮,每 i 个灯泡切换一次 ...
- LeetCode 319 ——Bulb Switcher——————【数学技巧】
319. Bulb Switcher My Submissions QuestionEditorial Solution Total Accepted: 15915 Total Submissions ...
- 【LeetCode】319. Bulb Switcher 解题报告(Python)
[LeetCode]319. Bulb Switcher 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/bulb ...
- 319. Bulb Switcher
题目: There are n bulbs that are initially off. You first turn on all the bulbs. Then, you turn off ev ...
- 319. Bulb Switcher——本质:迭代观察,然后找规律
There are n bulbs that are initially off. You first turn on all the bulbs. Then, you turn off every ...
- Java [Leetcode 319]Bulb Switcher
题目描述: There are n bulbs that are initially off. You first turn on all the bulbs. Then, you turn off ...
- LeetCode 319. Bulb Switcher
There are n bulbs that are initially off. You first turn on all the bulbs. Then, you turn off every ...
随机推荐
- 有关HTML的相关基础问题:
有关HTML的相关基础问题:1.Doctype作用?严格模式与混杂模式如何区分?它们有何意义? 1)<!DICTYPE>声明位于文档中的最前面,处于<html>标签之前,告 ...
- HDU 1423 最长上升公共子序列(LCIS)
题目大意: 给定两个数字数组a[] , b[],在这两个数组中找一个最长的公共上升子序列,输出最长的长度 从别人地方copy的= = LCIS理解: (1)f[i][j] 表示 a的前i,和b串前 j ...
- 【BZOJ2342】双倍回文(manacher,并查集)
题意: 思路:From http://blog.sina.com.cn/s/blog_8d5d2f04010196bh.html 首先我可以看出: (1)我们找到的串的本身也是一个回文串(显然) (2 ...
- [kuangbin带你飞]专题四 最短路练习 G MPI Maelstrom
#include<iostream> #include<cstring> #include<algorithm> #include<iomanip> # ...
- Nginx 源码
http://blog.sina.com.cn/s/articlelist_1834459124_1_1.html http://tengine.taobao.org/book/ https://gi ...
- Node & Express: some tips
1. 设置Express端口号: 在app.js中添加 app.set('port', process.env.PORT || 3000); 之后命令行中打入 PORT=1234 node app.j ...
- 【CV论文阅读】action recognition by dense trajectories
密集轨迹的方法是通过在视频帧上密集地采样像素点并且在追踪,从而构造视频的局部描述子,最后对视频进行分类的方法依然是传统的SVM等方法. 生成密集轨迹: (1)从8个不同的空间尺度中采样,它们的尺度差因 ...
- [C++]_[获取Utf8字符串的字符个数和子字符串]
场景: 1.有时候须要统计utf8字符串的个数,单纯统计字节个数是不行的. 2.有时候也须要获取从某个位置開始的n个连续字符用于显示或计算. static int GetUtf8LetterNumbe ...
- Date.parse函数的兼容问题
由于要用Date.parse函数解析一个日期时间的字符串.在谷歌浏览器中解析成功,但是IE.火狐中却不行. 不知何故.firefox对于类似"2010-12-20 15:55:00" ...
- map, string 强大的STL
hdu 1247 Hat's Words Input Standard input consists of a number of lowercase words, one per line, in ...