当前位置:网站首页>319. Bulb switch
319. Bulb switch
2022-07-05 08:35:00 【Mr Gao】
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 the second light bulb every two .
The third round , You switch the third bulb every three bulbs ( namely , Open to close , Turn off to turn on ). The first i round , Every time you i Switch the first light bulb i A light bulb switch . 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 .
Example 1:
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 .
Example 2:
Input :n = 0
Output :0
Example 3:
Input :n = 1
Output :1
Whether the light bulb is off here is related to his approximate number :
The conventional problem-solving code is as follows :
int f(int n){
int count=1;
int i;
for(i=2;i<=n/2;i++){
if(n%i==0){
count++;
}
}
return count;
}
int bulbSwitch(int n){
int i;
if(n==0){
return 0;
}
int count=1;
for(i=2;i<=n;i++){
int c=f(i);
// printf("%d ",c);
if(c%2==0){
count++;
}
}
return count;
}
The following is also a very good skill solution :
int bulbSwitch(int n){
int i;
if(n==0){
return 0;
}
int count=1;
for(i=2;i<=n;i++){
if(i*i>n){
return i-1;
}
}
return count;
}
边栏推荐
- FIO测试硬盘性能参数和实例详细总结(附源码)
- Count the number of inputs (C language)
- MySQL MHA high availability cluster
- 图解八道经典指针笔试题
- 猜谜语啦(11)
- The first week of summer vacation
- Cinq détails de conception du régulateur de tension linéaire
- Charge pump boost principle - this article will give you a simple understanding
- Run menu analysis
- How can fresh students write resumes to attract HR and interviewers
猜你喜欢

猜谜语啦(8)

Guess riddles (2)

剑指 Offer 09. 用两个栈实现队列
![[noi simulation] juice tree (tree DP)](/img/19/bc71e8dc3958e4cb87b31423a74617.png)
[noi simulation] juice tree (tree DP)

Talk about the function of magnetic beads in circuits

Apaas platform of TOP10 abroad

Take you to understand the working principle of lithium battery protection board

Keil use details -- magic wand

Stm32--- systick timer

实例005:三数排序 输入三个整数x,y,z,请把这三个数由小到大输出。
随机推荐
Example 007: copy data from one list to another list.
Illustration of eight classic pointer written test questions
关于线性稳压器的五个设计细节
Example 004: for the day of the day, enter a day of a month of a year to judge the day of the year?
剑指 Offer 06. 从尾到头打印链表
MATLAB小技巧(28)模糊綜合評價
每日一题——输入一个日期,输出它是该年的第几天
Example 002: the bonus paid by the "individual income tax calculation" enterprise is based on the profit commission. When the profit (I) is less than or equal to 100000 yuan, the bonus can be increase
轮子1:QCustomPlot初始化模板
2020-05-21
U8g2 drawing
猜谜语啦(6)
Arrangement of some library files
STM32 single chip microcomputer - bit band operation
实例003:完全平方数 一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问该数是多少?
Keil use details -- magic wand
Arduino burning program and Arduino burning bootloader
猜谜语啦(142)
Business modeling of software model | vision
Arduino operation stm32