当前位置:网站首页>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
边栏推荐
- . NETCORE sets the API post mode, which can accept parameters directly in parentheses
- Log4net is in Net core usage
- socket編程 send()與 recv()函數詳解
- Quartz. Www. 18fu Used in net core
- IDEA查看历史记录【文件历史和项目历史】
- Unity knowledge points (ugui 2)
- 移植tslib时ts_setup: No such file or directory、ts_open: No such file or director
- Build websocket server in. Net5 webapi
- QT dynamic translation of Chinese and English languages
- Idempotency practice operation, explaining idempotency based on business
猜你喜欢

Node configuration environment CMD does not take effect

2022 group programming ladder simulation l2-1 blind box packaging line (25 points)

QT dynamic translation of Chinese and English languages

【微服务】(十)—— 统一网关Gateway

Design and implementation of redis (1): understand data structures and objects

Codeforces round 784 (Div. 4) (AK CF (XD) for the first time)

AWS from entry to actual combat: creating accounts

Super easy to use asynchronous export function of Excel
![Idea view history [file history and project history]](/img/b2/3128105eca7449c55146ce3b9e5c2e.png)
Idea view history [file history and project history]

Using swagger in. Net5
随机推荐
Punch in: 4.23 C language chapter - (1) first knowledge of C language - (12) structure
Fiddler use
Problem C: Hanoi Tower III
Oracle query foreign keys contain comma separated data
ThreadLocal 测试多线程变量实例
Commonly used classes
Design and implementation of redis (3): persistence strategy RDB, AOF
WinForm allows the form form to switch between the front and active states
Three types of cyclic structure
The fourth operation
socket編程 send()與 recv()函數詳解
TCP three handshakes and four waves
Codeforces Round #784 (Div. 4)题解 (第一次AK cf (XD
Section 1 array and slicing in Chapter 6
MySQL keyword group_ Concat, combined connection query
2022 group programming ladder simulation match 1-8 are prime numbers (20 points)
Unity basics 2
js 中,为一个里面带有input 的label 绑定事件后在父元素绑定单机事件,事件执行两次,求解
2022 团体程序设计天梯赛 模拟赛 L2-4 哲哲打游戏 (25 分)
C-11 problem h: treasure chest 2