本文主要是介绍leetcode-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 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:
思路:就是判断1-n中因式分解因式的个数为奇数的个数。素数为2,一般的合数也是偶数个,只有有两个相同因式的数为奇数,即某个数的平方。
class Solution {
public:int bulbSwitch(int n) {//就是判断每个数因式分解的个数// 质数只有1和他本身,所以最后为off//合数要想总的因数为奇数,那么肯定有两个因式一样,即这个数肯定是某个数的平方//所以本体就是求k^2<=n中k的个数return sqrt(n);}
};
这篇关于leetcode-319. Bulb Switcher的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!