当前位置:网站首页>Leetcode punch in diary day 01
Leetcode punch in diary day 01
2022-04-23 03:30:00 【Jack_ joker】
leetcode 319 Bulb switch
Initially there is n One bulb is off . The first round , You'll turn on all the lights . The next round , You will turn off one light bulb every two .
The third round , You switch one light bulb every three light bulbs ( namely , Open to close , Turn off to turn on ). The first i round , Every time you i A light bulb is a switch that switches one light bulb . Until the first n round , You just need to switch the last light bulb .
Find out and return to n How many light bulbs are there behind the wheel .
Input :n = 3
Output :1
explain :
At the beginning , Lamp status [ close , close , close ].
After the first round , Lamp status [ Turn on , Turn on , Turn on ].
After the second round , Lamp status [ Turn on , close , Turn on ].
After the third round , Lamp status [ Turn on , close , close ].
You should go back 1, Because only one bulb is still on .
Input :n = 0
Output :0
The first time I saw this question , The idea is to simulate the whole process of bulb switching , But finally I saw the solution , I'm a fool .
Mathematical methods :
therefore , For the first k A light bulb , The number of times it is switched is exactly k The approximate number of . If k There are even divisors , So finally, the third k The state of each bulb is dark ; If k There are odd divisors , So finally, the third k The state of each bulb is on .
about k for , If it has a divisor x, Then there must be a divisor k/x, So just be x^2 It's not equal to k, Divisors are 「 Pair 」 The emergence of . This means that , Only when k yes 「 Complete square 」 when , It will have odd divisors , Otherwise, there must be even divisors .
So we just need to find out 1, 2, 3,⋯,n The number of complete squares in , The answer is sqrt(n);
The little detail is , In order to avoid accuracy problems , It can be changed to sqrt(n + 0.5); Make sure the rounding down range is correct .
版权声明
本文为[Jack_ joker]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220607497525.html
边栏推荐
- C interface
- L3-011 直捣黄龙 (30 分)
- 幂等性实践操作,基于业务讲解幂等性
- 打卡:4.23 C语言篇 -(1)初识C语言 - (12)结构体
- socket編程 send()與 recv()函數詳解
- Applet - canvas drawing Poster
- 【微服务】(十)—— 统一网关Gateway
- Super easy to use asynchronous export function of Excel
- AWS from entry to actual combat: creating accounts
- Redis (17) -- redis cache related problem solving
猜你喜欢
浅学一下I/O流和File类文件操作
PyMOL usage
2022 group programming ladder game simulation L2-4 Zhezhi game (25 points)
Supersocket is Use in net5 - concept
The principle and solution of not allowing pasting in an English Network
Section 2 map and structure in Chapter 6
[microservices] (x) -- Unified gateway
【微服务】(十)—— 统一网关Gateway
Unity basics 2
淺學一下I/O流和File類文件操作
随机推荐
There is no index in the database table. When inserting data, SQL statements are used to prevent repeated addition (Reprint)
JS inheritance
Problem C: realize Joseph Ring with linked list
[microservices] (x) -- Unified gateway
Codeforces Round #784 (Div. 4)題解 (第一次AK cf (XD
批量下載文件----壓縮後再下載
Applet - WXS
Database - MySQL -- Navicat import SQL error 1067 - invalid default value for 'paydate‘
JS - accuracy issues
WinForm allows the form form to switch between the front and active states
Advanced sorting - fast sorting
Unity knowledge points (ugui)
Batch download of files ---- compressed and then downloaded
数据库表中不建索引,在插入数据时,通过sql语句防止重复添加(转载)
Codeforces Round #784 (Div. 4)题解 (第一次AK cf (XD
通过 zxing 生成二维码
C interface
you need to be root to perform this command
集合之List接口
MySQL query specifies that a row is sorted to the first row