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.

 /*************************************************************************
> File Name: LeetCode319.c
> Author: Juntaran
> Mail: Jacinthmail@gmail.com
> Created Time: Tue 10 May 2016 05:52:58 PM CST
************************************************************************/ /************************************************************************* 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. ************************************************************************/ #include <stdio.h> int bulbSwitch( int n )
{
int i;
while( i*i <= n )
{
i++;
}
return i-;
} int main()
{
int n = ;
int ret = bulbSwitch(n);
printf("%d\n", ret);
return ;
}

LeetCode 319的更多相关文章

  1. LeetCode 319 ——Bulb Switcher——————【数学技巧】

    319. Bulb Switcher My Submissions QuestionEditorial Solution Total Accepted: 15915 Total Submissions ...

  2. Java实现 LeetCode 319 灯泡开关

    319. 灯泡开关 初始时有 n 个灯泡关闭. 第 1 轮,你打开所有的灯泡. 第 2 轮,每两个灯泡你关闭一次. 第 3 轮,每三个灯泡切换一次开关(如果关闭则开启,如果开启则关闭).第 i 轮,每 ...

  3. Java [Leetcode 319]Bulb Switcher

    题目描述: There are n bulbs that are initially off. You first turn on all the bulbs. Then, you turn off ...

  4. LeetCode 319. Bulb Switcher

    There are n bulbs that are initially off. You first turn on all the bulbs. Then, you turn off every ...

  5. leetcode 319 灯泡问题

    例子:1-9 1的因子1 2       1,2 3        1,,3 4        1,2,4 5        1,5 6        1,2,3,6 7        1,7 8   ...

  6. Leetcode 319.灯泡开关

    灯泡开关 初始时有 n 个灯泡关闭.第 1 轮,你打开所有的灯泡.第 2 轮,每两个灯泡你关闭一次.第 3 轮,每三个灯泡切换一次开关(如果关闭则开启,如果开启则关闭).第 i 轮,每 i 个灯泡切换 ...

  7. Leetcode 319 Bulb Switcher 找规律

    有n盏关着的灯,第k轮把序号为k倍数的关着的灯打开,开着的灯关闭. class Solution { public: int bulbSwitch(int n) { return (int)sqrt( ...

  8. [LeetCode]319. Bulb Switcher灯泡开关

    智商压制的一道题 这个题有个数学定理: 一般数(非完全平方数)的因子有偶数个 完全平凡数的因子有奇数个 开开关的时候,第i个灯每到它的因子一轮的时候就会拨动一下,也就是每个灯拨动的次数是它的因子数 而 ...

  9. leetcode & lintcode for bug-free

    刷题备忘录,for bug-free leetcode 396. Rotate Function 题意: Given an array of integers A and let n to be it ...

随机推荐

  1. HDU 4901 The Romantic Hero (计数DP)

    The Romantic Hero 题目链接: http://acm.hust.edu.cn/vjudge/contest/121349#problem/E Description There is ...

  2. Laravel Controllers

    Basic Controllers Instead of defining all of your route-level logic in a single routes.php file, you ...

  3. UVA 11300 Spreading the Wealth

    题目大意:n个人手中有些金币,每个人可给相邻两个人一些金币,使得最终每个人手中金币数相同,求被转手的金币最少数 m为最终每个人手中的金币数,a1,a2,a3,...,an为每个人开始时手中的金币数,x ...

  4. codeforces 630F Selection of Personnel(组合数)

    F. Selection of Personnel time limit per test 0.5 seconds memory limit per test 64 megabytes input s ...

  5. C++获取系统的Mac地址

    C++获取系统的Mac地址,加上libnetapi32.a #include <windows.h> #include <stdlib.h> #include <stdi ...

  6. CodeForces 589I Lottery (暴力,水题)

    题意:给定 n 和 k,然后是 n 个数,表示1-k的一个值,问你修改最少的数,使得所有的1-k的数目都等于n/k. 析:水题,只要用每个数减去n/k,然后取模,加起来除以2,就ok了. 代码如下: ...

  7. Webstorm2016激活码[ 转]

    转至:http://blog.csdn.net/tingwode2014_/article/details/51063657 43B4A73YYJ-eyJsaWNlbnNlSWQiOiI0M0I0QT ...

  8. sqlserver2012

    sqlserver2012作死的节奏啊,微软最近怎么了?搞个sqlserver2012死活安装不上,无论如何都是.NET Framework 4报错,机器本身的版本比安装包的高就报错?微软的程序员思维 ...

  9. js中的if判断十分优美的简洁写法

    本尊混迹猿人类也有5年有余,从最开始的C#到java再到php到至今的python,不能说精通,也算得上是熟悉,对各个语言的语法也算是了解. 虽然目前在开发web程序,了解一些java知识,但是今天在 ...

  10. 【M2】最好使用C++转型操作符

    1.C语言中的转型操作符有两个问题: a.是个通用的转换操作符,也就是说,可以从一个类型转换到其他类型.通用必定是低效率和冗余的,因为要考虑很多情况. b.在代码中,难以辨认出哪些是类型转换. 2.C ...