319_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.

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.


第一轮,全部打开
第二轮,转动2,4,6, …… , 2n的开关
第三轮,转动3,6,9, …… , 3n的开关
第四轮,转动4,8,12, ……, 4n的开关

……


某开关转动奇数次则是关闭,转动偶数次则为打开。因为最开始第一轮时打开了所有。
某个数字a = b * b(b的平方),该数字两个因数只出现一次,为奇数,所以为开

int bulbSwitch(int n) {
    int result = 0;
    for(int i = 1; i * i <= n; i++)
    {
        result++;
    }
    return result;
}
 
原文地址:https://www.cnblogs.com/Anthony-Wang/p/5098598.html