国产探花免费观看_亚洲丰满少妇自慰呻吟_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ā)表
主站蜘蛛池模板: 西丰县| 长岭县| 西畴县| 松江区| 沙田区| 丽水市| 五大连池市| 三亚市| 鹤庆县| 平乐县| 淮南市| 资溪县| 宁晋县| 叙永县| 道真| 若尔盖县| 分宜县| 比如县| 娱乐| 乳山市| 巴彦淖尔市| 台中县| 清新县| 黄龙县| 房产| 白城市| 苗栗县| 富源县| 济源市| 公主岭市| 太仓市| 格尔木市| 黑水县| 全州县| 榆社县| 淮滨县| 汝城县| 东莞市| 温州市| 赣榆县| 贵南县|