[LeetCode] Bulb Switcher
2016-03-06 10:39
281 查看
前言
Bulb Switcher是LeetCode上的一道”脑筋急转弯”类题目,颇有趣味性。难度并不大,实际上想通数学规律后,此题只需要一行代码。题目
https://leetcode.com/problems/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 ith round, you toggle every i bulb. For the nth round, you only toggle the last bulb. Find how many bulbs are on after n rounds.
Example:
Given n = 3.
At first, the three bulbs are [off, off, off].
After first round, the three bulbs are [on, on, on].
After second round, the three bulbs are [on, off, on].
After third round, the three bulbs are [on, off, off].
So you should return 1, because there is only one bulb is on.
题意很清楚,就是第i次操作,把每i个灯泡中的最后一个转换一下状态。问最后还亮着的灯多少个。
分析
一开始做这个题,并没有想太多,就是手写了几个,找点规律。很容易就会发现——0 : 0
1 - 3 : 1
4 - 8 : 2
9 - 15 : 3
16 - 24 :4
……
于是根据这样的规律,可以写出这样的代码:
class Solution { public: int bulbSwitch(int n) { int i = 0; for (; i*(i+2) < n; i++) {} return i; } };
不过这样还是太繁琐了点,仔细想想,在所有操作中,被变换了奇数次状态的灯泡,会是亮着的。而被变换了偶数次状态的灯泡最后都灭了。又由于题目每次的操作是“转换每i个灯泡中的最后一个”,所以很明显是一道与因数分解有关的问题。
每个数字都有1和它本身这两个基本的因数,有多少个因数决定了这个数字所代表的位置的灯泡被转换多少次。观察下表:
1 ——— 1
2 ——— 1, 2
3 ——— 1, 3
4 ——— 1, 2, 4
5 ——— 1, 5
6 ——— 1, 2, 3, 6
7 ——— 1, 7
8 ——— 1, 2, 4, 8
9 ——— 1, 3, 9
我们发现,只有平方数的因子才是奇数个,那么这些位置的灯泡最后会亮着。所以我们只要根据给出的n得到不超过n的数字中有多少个平方数即可,而得到这个数字只需要计算n的平方根,舍弃小数部分即可。
代码
class Solution { public: int bulbSwitch(int n) { return sqrt(n); } };
相关文章推荐
- 用批处理解决数学问题的代码第1/4页
- php常用数学函数汇总
- 无限循环小数
- 数学书籍备忘
- leetcode 179 Largest Number
- leetcode 24 Swap Nodes in Pairs
- leetcode 2 Add Two Numbers 方法1
- leetcode 2 Add Two Numbers 方法2
- 建立以人为本的数学课程观
- 文兴小学2008-2009学年度第一学期数学科教学计划
- MySql函数大全<二>
- 一生受用的数学公式
- oracle中行列转换
- 数学猜想
- 世界第一数学强校的背后
- 数学是一种思考方式
- 【巧用数学面板达成书写表达式】
- C语言产生随机数
- shell浮点运算工具bc
- 论数学