zoukankan      html  css  js  c++  java
  • [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 i-th round, you toggle every i bulb. For the n-th round, you only toggle the last bulb. Find how many bulbs are on after nrounds.

    Example:

    Input: 3
    Output: 1 
    Explanation: 
    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.

    题意

    Solution1

    code

     1 /**
     2 factor of 6: 1,2,3,6
     3 factor of 7: 1,7
     4 factor of 9: 1,3,9
     5 
     6 so all number have even number of factors except square number(e.g: factor of 9:1,3,9).
     7 square number must turn on because of odd number of factors(9: turn on at 1st, off at 3rd, on at 9th)
     8 other number must turn off(6: turn on at 1st, off at 2nd, on at 3rd, off at 6th)
     9 so we only need to compute the number of square number less equal than n
    10 **/
    11 
    12 public class Solution {
    13     public int bulbSwitch(int n) {
    14         return (int)Math.sqrt(n);
    15     }
    16 }
  • 相关阅读:
    文件上传upload-lab
    windows缓冲区溢出
    c++头文件创建与使用
    bash脚本中一些变量的含义
    bash脚本一些语句的语法
    一些暴力破解的工具和字典生成文件
    Netcat基本操作命令
    Trevorc2
    msfconsol中的部分模块
    msfvenom木马生成
  • 原文地址:https://www.cnblogs.com/liuliu5151/p/10823262.html
Copyright © 2011-2022 走看看