当前位置:网站首页>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;
}
边栏推荐
- 每日一题——输入一个日期,输出它是该年的第几天
- 猜谜语啦(5)
- leetcode - 445. Add two numbers II
- 剑指 Offer 06. 从尾到头打印链表
- MATLAB小技巧(28)模糊综合评价
- Installation and use of libjpeg and ligpng
- Several problems to be considered and solved in the design of multi tenant architecture
- STM32---ADC
- An enterprise information integration system
- Example 003: a complete square is an integer. It is a complete square after adding 100, and it is a complete square after adding 168. What is the number?
猜你喜欢
实例003:完全平方数 一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问该数是多少?
Keil use details -- magic wand
On boost circuit
Lori remote control commissioning record
[NAS1](2021CVPR)AttentiveNAS: Improving Neural Architecture Search via Attentive Sampling (未完)
Example 008: 99 multiplication table
图解八道经典指针笔试题
Example 007: copy data from one list to another list.
猜谜语啦(10)
How to write cover letter?
随机推荐
Several problems to be considered and solved in the design of multi tenant architecture
Esphone Feixun DC1 soft change access homeassstant
Cmder of win artifact
How can fresh students write resumes to attract HR and interviewers
Count the number of inputs (C language)
2022.7.4-----leetcode.1200
Example 004: for the day of the day, enter a day of a month of a year to judge the day of the year?
Several important parameters of LDO circuit design and type selection
Arrangement of some library files
Example 003: a complete square is an integer. It is a complete square after adding 100, and it is a complete square after adding 168. What is the number?
Arduino operation stm32
Explore the authentication mechanism of StarUML
实例004:这天第几天 输入某年某月某日,判断这一天是这一年的第几天?
【NOI模拟赛】汁树(树形DP)
Various types of questions judged by prime numbers within 100 (C language)
整形的分类:short in long longlong
319. 灯泡开关
Is the security account given by Yixue school safe? Where can I open an account
Search data in geo database
Buildroot system for making raspberry pie cm3