zoukankan      html  css  js  c++  java
  • 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 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.


    第一轮,全部打开
    第二轮,转动2,4,6, …… , 2n的开关
    第三轮,转动3,6,9, …… , 3n的开关
    第四轮,转动4,8,12, ……, 4n的开关

    ……


    某开关转动奇数次则是关闭,转动偶数次则为打开。因为最开始第一轮时打开了所有。
    某个数字a = b * b(b的平方),该数字两个因数只出现一次,为奇数,所以为开

    int bulbSwitch(int n) {
        int result = 0;
        for(int i = 1; i * i <= n; i++)
        {
            result++;
        }
        return result;
    }
     
  • 相关阅读:
    我为何需要使用空接口?
    Castle 整合.NET Remoting
    MVC结构简介
    在asp.net页面上得到Castle容器的实例
    Castle.MVC框架介绍
    08.vue-router动态路由匹配
    07. vue-router嵌套路由
    06.路由重定向
    04 Vue Router路由管理器
    ES6新特性之 let 、const
  • 原文地址:https://www.cnblogs.com/Anthony-Wang/p/5098598.html
Copyright © 2011-2022 走看看