当前位置:网站首页>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
边栏推荐
- IDEA查看历史记录【文件历史和项目历史】
- Three column layout (fixed width on both sides in the middle and fixed width on both sides in the middle)
- PyMOL usage
- 深度学习笔记(二)——激活函数原理与实现
- Chapter 9 of C language programming (fifth edition of Tan Haoqiang) analysis and answer of exercises for users to establish their own data types
- MySQL query specifies that a row is sorted to the first row
- 【微服务】(十)—— 统一网关Gateway
- What to pay attention to when writing the first code
- ThreadLocal 测试多线程变量实例
- Quartz. Www. 18fu Used in net core
猜你喜欢

Visual programming - Experiment 2

Unity games and related interview questions

New ORM framework -- Introduction to beetlsql

Super easy to use asynchronous export function of Excel

Node configuration environment CMD does not take effect

Punch in: 4.22 C language chapter - (1) first knowledge of C language - (11) pointer

Section 1 array and slicing in Chapter 6

Chapter 9 of C language programming (fifth edition of Tan Haoqiang) analysis and answer of exercises for users to establish their own data types

关于idea调试模式下启动特别慢的优化

Deep learning notes (II) -- principle and implementation of activation function
随机推荐
MySQL keyword group_ Concat, combined connection query
淺學一下I/O流和File類文件操作
Codeforces Round #784 (Div. 4)題解 (第一次AK cf (XD
Talent Plan 学习营初体验:交流+坚持 开源协作课程学习的不二路径
Redis (17) -- redis cache related problem solving
Un aperçu des flux d'E / s et des opérations de fichiers de classe de fichiers
2022 团体程序设计天梯赛 模拟赛 L2-1 盲盒包装流水线 (25 分)
Section 1 array and slicing in Chapter 6
Punch in: 4.23 C language chapter - (1) first knowledge of C language - (12) structure
ThreadLocal test multithreaded variable instance
The query type of MySQL is very inefficient.
JS calculates the display date according to the time
Super easy to use [general excel import function]
Generate QR code through zxing
PWA I'm here
Log4net is in Net core usage
Translation of l1-7 matrix columns in 2022 group programming ladder Simulation Competition (20 points)
全新的ORM框架——BeetlSQL介绍
Definition format of array
What to pay attention to when writing the first code