当前位置:网站首页>319. 灯泡开关
319. 灯泡开关
2022-07-05 08:31:00 【Mr Gao】
319. 灯泡开关
初始时有 n 个灯泡处于关闭状态。第一轮,你将会打开所有灯泡。接下来的第二轮,你将会每两个灯泡关闭第二个。
第三轮,你每三个灯泡就切换第三个灯泡的开关(即,打开变关闭,关闭变打开)。第 i 轮,你每 i 个灯泡就切换第 i 个灯泡的开关。直到第 n 轮,你只需要切换最后一个灯泡的开关。
找出并返回 n 轮后有多少个亮着的灯泡。
示例 1:
输入:n = 3
输出:1
解释:
初始时, 灯泡状态 [关闭, 关闭, 关闭].
第一轮后, 灯泡状态 [开启, 开启, 开启].
第二轮后, 灯泡状态 [开启, 关闭, 开启].
第三轮后, 灯泡状态 [开启, 关闭, 关闭].
你应该返回 1,因为只有一个灯泡还亮着。
示例 2:
输入:n = 0
输出:0
示例 3:
输入:n = 1
输出:1
这里灯泡是否关闭和他的约数个数有关:
常规解题代码如下:
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;
}
下面还由一种非常棒的技巧解法:
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;
}
边栏推荐
- Esphone retrofits old fans
- UE像素流,来颗“减肥药”吧!
- Talk about the function of magnetic beads in circuits
- 实例005:三数排序 输入三个整数x,y,z,请把这三个数由小到大输出。
- Shell script
- Meizu Bluetooth remote control temperature and humidity access homeassistant
- Explication de la procédure stockée pour SQL Server
- 【云原生 | 从零开始学Kubernetes】三、Kubernetes集群管理工具kubectl
- 【论文阅读】2022年最新迁移学习综述笔注(Transferability in Deep Learning: A Survey)
- MHA High available Cluster for MySQL
猜你喜欢

My-basic application 2: my-basic installation and operation

QEMU STM32 vscode debugging environment configuration

Semiconductor devices (III) FET

Stablq of linked list

OC and OD gate circuit

MATLAB小技巧(28)模糊综合评价

实例003:完全平方数 一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问该数是多少?

STM32 lights up the 1.8-inch screen under Arduino IDE

Several important parameters of LDO circuit design and type selection
![[nas1] (2021cvpr) attentivenas: improving neural architecture search via attentive sampling (unfinished)](/img/3b/c94b8466370f4461875c85b4f66860.png)
[nas1] (2021cvpr) attentivenas: improving neural architecture search via attentive sampling (unfinished)
随机推荐
实例003:完全平方数 一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问该数是多少?
STM32---IIC
Daily question - input a date and output the day of the year
Sword finger offer 09 Implementing queues with two stacks
实例002:“个税计算” 企业发放的奖金根据利润提成。利润(I)低于或等于10万元时,奖金可提10%;利润高于10万元,低于20万元时,低于10万元的部分按10%提成,高于10万元的部分,可提成7.
Google sitemap files for rails Projects - Google sitemap files for rails projects
Example 004: for the day of the day, enter a day of a month of a year to judge the day of the year?
leetcode - 445. 两数相加 II
实例008:九九乘法表
[tutorial 19 of trio basic from introduction to proficiency] detailed introduction of trio as a slave station connecting to the third-party bus (anybus PROFIBUS DP...)
Basic information commands and functions of kernel development
实例010:给人看的时间
Arduino operation stm32
如何写Cover Letter?
Explication de la procédure stockée pour SQL Server
PIP installation
Esphone retrofits old fans
实例007:copy 将一个列表的数据复制到另一个列表中。
More than 90% of hardware engineers will encounter problems when MOS tubes are burned out!
Management and use of DokuWiki (supplementary)