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; } };