国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

319. Bulb Switcher

2019-11-08 03:23:38
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

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.

s思路: 1. 老題了,畫(huà)一下,就發(fā)現(xiàn)是個(gè)數(shù)學(xué)題。例如:6號(hào)這個(gè)燈泡,在第1次,第6次一開(kāi)一關(guān),在第2次,第3次一開(kāi)一關(guān),最后就回到了初始狀態(tài),用數(shù)學(xué)語(yǔ)言來(lái)說(shuō):6=1*6, 6還可以等于2*3,因此成對(duì)的操作就相互抵消了;那最后怎么還有燈泡是打開(kāi)的呢?比如:4=1*4,4還可以等于2*2,看見(jiàn)沒(méi),4因?yàn)槭峭耆椒剑栽?的時(shí)候只能操作一次而不能抵消,因此最后打開(kāi)的燈泡都是完全平方數(shù)的位置。 2. 起始看到隔多少個(gè)燈泡操作一次,這個(gè)隔,也就表示了乘法的意思!

//方法1:老實(shí)版本class Solution {public: int bulbSwitch(int n) { int count=0; for(int i=1;i*i<=n;i++){ count++; } return count; }};//方法2:有點(diǎn)沒(méi)想到,還可以這么猥瑣class Solution {public: int bulbSwitch(int n) { return sqrt(n); }};
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 台安县| 海盐县| 靖州| 洮南市| 济南市| 察哈| 泰安市| 隆林| 安阳市| 年辖:市辖区| 防城港市| 梓潼县| 成武县| 安福县| 茌平县| 容城县| 绿春县| 郴州市| 海安县| 株洲市| 东光县| 偏关县| 延津县| 县级市| 富民县| 都安| 昌吉市| 贵定县| 文昌市| 上思县| 固镇县| 奉化市| 正蓝旗| 白朗县| 汪清县| 江达县| 陕西省| 浠水县| 安岳县| 灵寿县| 图们市|