您的位置:首页 > 编程语言 > C语言/C++

LeetCode:319. Bulb Switcher、一行C语言表达式判断给定的整数是否是一个2的幂

2016-07-25 20:57 423 查看

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:

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.

class Solution {
public:
int bulbSwitch(int n) {
return sqrt(n);
//题目都没有看懂竟然能够解题
//每两个:第二个,第四个,
//每三个:第三个,第六个
// if(!n)  return 0;
// int i=1;
// while(n>=i*i)    ++i;
// return i-1;
}
};


一行C语言表达式判断给定的整数是否是一个2的幂判断给定的整数是否是一个2的幂

8、 给出一行C语言表达式,判断给定的整数是否是一个2的幂。
答案:(b & (b-1)) == 0 http://blog.csdn.net/wangran51/article/details/8782980
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  leetcode