当前位置:网站首页>There is a problem with the light switch from 1 to 100
There is a problem with the light switch from 1 to 100
2022-04-23 16:08:00 【Learning is boundless, happy to make a boat】
Yes 1 To 100 No. 1 light , All lit up at first . Each light has a separate switch , And the switch has only “ open ” and “ Turn off ” Two kinds of state .
Put all... For the first time 1 Press the switch of multiple lamp once , The second time, put all 2 Press the switch of the multiple lamp , Until the hundredth time 100 Press the switch of the multiple lamp .
ask , How many lights are still off ?
One 、Java Programming to solve
package test;
import java.util.ArrayList;
import java.util.List;
public class lamp1_100 {
public static void main(String[] args){
int count=0;
List<Integer> dd = new ArrayList<Integer>();
// All lights are on by default
for(int i=0;i<100;i++){
dd.add(1);
}
// Operate all lights
for(int k=1;k<=100;k++){
// Yes k If the operation is set to the on multiple If it is on, set it to off
for(int t=1;t<=100/k;t++){
if(dd.get((k*t)-1)==0){
dd.set((k*t)-1,1);
}else{
dd.set((k*t)-1,0);
}
}
}
// Count the number of lights that are off
for(int i=0;i<100;i++){
if(dd.get(i)==0){
count++;
System.out.println(" The remaining index lights are off :"+ i);
}
}
System.out.println(" Number of lights remaining off :"+count);
}
}
Two 、python The dictionary solves
The bulb has two states : On and off , use -1 On behalf of Guan , use 1 On behalf of . After each operation , Multiply the state by -1.
on ---> 1 off ---> -1
dic = {k:1 for k in range(1,101)}
def lanm(n):
for i in range(1,n+1):
for x in dic:
if x%i == 0:
dic[x] = int(dic[x])*-1
return dic
num = len({k: v for k,v in lanm(100).items() if v == 1})
print(f"There are {num} lights on.")
#There are 10 lights on.
3、 ... and 、python list
lamp = []
# Default 100 All the lights are on
for i in range(100):
lamp.append(1)
# loop 100 Lights
for k in range(1,101):
i = 0
# Yes k Multiple of the lamp for switching operation
while i <= 100/k:
if lamp[(i*k)-1] == 0:
lamp[(i * k) - 1] = 1
else:
lamp[(i*k)-1]=0
i += 1
#count = len([i for i in lamp if i==0])
count = 0
for i in range(0,100):
if lamp[i] == 0:
count +=1
print(f" Where the lights are off {i+1}")
print(f" Number of lights off {count}")
版权声明
本文为[Learning is boundless, happy to make a boat]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231403594313.html
边栏推荐
- 最详细的背包问题!!!
- MySQL的btree索引和hash索引区别
- Start Oracle service on Linux
- Merging of Shanzhai version [i]
- The most detailed Backpack issues!!!
- Government cloud migration practice: Beiming digital division used hypermotion cloud migration products to implement the cloud migration project for a government unit, and completed the migration of n
- Spark 算子之filter使用
- shell_2
- JSP learning 1
- Hyperbdr cloud disaster recovery v3 Release of version 3.0 | upgrade of disaster recovery function and optimization of resource group management function
猜你喜欢
Spark 算子之sortBy使用
面试题 17.10. 主要元素
捡起MATLAB的第(6)天
R语言中绘制ROC曲线方法二:pROC包
Gartner predicts that the scale of cloud migration will increase significantly; What are the advantages of cloud migration?
Hyperbdr cloud disaster recovery v3 Release of version 3.0 | upgrade of disaster recovery function and optimization of resource group management function
Construction of esp32 compilation environment
Countdown 1 day ~ 2022 online conference of cloud disaster tolerance products is about to begin
Config learning notes component
Website pressure measurement tools Apache AB, webbench, Apache jemeter
随机推荐
捡起MATLAB的第(10)天
js正则判断域名或者IP的端口路径是否正确
Leetcode-374 guess the size of the number
Hyperbdr cloud disaster recovery v3 Version 2.1 release supports more cloud platforms and adds monitoring and alarm functions
[open source tool sharing] MCU debugging assistant (oscillograph / modification / log) - linkscope
Cloudy data flow? Disaster recovery on cloud? Last value content sharing years ago
You need to know about cloud disaster recovery
面试题 17.10. 主要元素
Implement default page
How important is the operation and maintenance process? I heard it can save 2 million a year?
[key points of final review of modern electronic assembly]
JS regular détermine si le nom de domaine ou le chemin de port IP est correct
Construction of esp32 compilation environment
ESXi封装网卡驱动
Grbl learning (I)
What does cloud disaster tolerance mean? What is the difference between cloud disaster tolerance and traditional disaster tolerance?
捡起MATLAB的第(3)天
Day (7) of picking up matlab
The most detailed Backpack issues!!!
Fastjon2 here he is, the performance is significantly improved, and he can fight for another ten years