当前位置:网站首页>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
边栏推荐
- Redis (17) -- redis cache related problem solving
- Codeforces Round #784 (Div. 4)題解 (第一次AK cf (XD
- Deep learning notes (II) -- principle and implementation of activation function
- The principle and solution of not allowing pasting in an English Network
- 浅学一下I/O流和File类文件操作
- Common exceptions
- Super easy to use [general excel import function]
- MySQL installation pit
- 2022 团体程序设计天梯赛 模拟赛 L1-7 矩阵列平移 (20 分)
- 移植tslib时ts_setup: No such file or directory、ts_open: No such file or director
猜你喜欢

Test questions (2)

2022 团体程序设计天梯赛 模拟赛 L2-1 盲盒包装流水线 (25 分)

Explanation keyword of MySQL

Unity Basics

Supersocket is Use in net5 - concept

IDEA查看历史记录【文件历史和项目历史】

全新的ORM框架——BeetlSQL介绍

Chapter 7 of C language programming (fifth edition of Tan Haoqiang) analysis and answer of modular programming exercises with functions

Database - MySQL -- Navicat import SQL error 1067 - invalid default value for 'paydate‘
![Idea view history [file history and project history]](/img/b2/3128105eca7449c55146ce3b9e5c2e.png)
Idea view history [file history and project history]
随机推荐
MySQL索引详解【B+Tree索引、哈希索引、全文索引、覆盖索引】
The query type of MySQL is very inefficient.
L3-011 direct attack Huanglong (30 points)
Identifier and type conversion
socket编程 send()与 recv()函数详解
Query stored procedures in PostgreSQL
Supersocket is Use in net5 - startup
poi根据数据创建导出excel
数据库表中不建索引,在插入数据时,通过sql语句防止重复添加(转载)
2022 团体程序设计天梯赛 模拟赛 1-8 均是素数 (20 分)
Visual programming -- how to customize the mouse cursor
Quartz. Www. 18fu Used in net core
Idea view history [file history and project history]
Applet - canvas drawing Poster
WinForm allows the form form to switch between the front and active states
批量下載文件----壓縮後再下載
Common exceptions
QT learning summary
2022 group programming ladder game simulation L2-4 Zhezhi game (25 points)
String input problem