当前位置:网站首页>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;
}
边栏推荐
猜你喜欢
Negative pressure generation of buck-boost circuit
Daily question - input a date and output the day of the year
猜谜语啦(7)
Simple design description of MIC circuit of ECM mobile phone
[three tier architecture]
Illustration of eight classic pointer written test questions
Installation and use of libjpeg and ligpng
【NOI模拟赛】汁树(树形DP)
Classic application of MOS transistor circuit design (1) -iic bidirectional level shift
【三层架构】
随机推荐
Example 005: three numbers sorting input three integers x, y, Z, please output these three numbers from small to large.
实例007:copy 将一个列表的数据复制到另一个列表中。
亿学学堂给的证券账户安不安全?哪里可以开户
猜谜语啦(7)
STM32 --- configuration of external interrupt
STM32 --- GPIO configuration & GPIO related library functions
Briefly talk about the identification protocol of mobile port -bc1.2
Summary of SIM card circuit knowledge
Search data in geo database
剑指 Offer 05. 替换空格
Talk about the function of magnetic beads in circuits
Esp8266 interrupt configuration
实例004:这天第几天 输入某年某月某日,判断这一天是这一年的第几天?
STM32 single chip microcomputer -- debug in keil5 cannot enter the main function
Shell script
Several implementation schemes of anti reverse connection protection of positive and negative poles of power supply!
GEO数据库中搜索数据
UE pixel stream, come to a "diet pill"!
STM32 --- serial port communication
STM32---ADC