当前位置:网站首页>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
边栏推荐
猜你喜欢
随机推荐
leetcode-374 猜数字大小
多线程原理和常用方法以及Thread和Runnable的区别
第九天 static 抽象类 接口
捡起MATLAB的第(3)天
VIM specifies the line comment and reconciliation comment
Sort by character occurrence frequency 451
JSP learning 2
How to conduct application security test (AST)
Master vscode remote GDB debugging
[section 5 if and for]
如何进行应用安全测试(AST)
Install redis and deploy redis high availability cluster
捡起MATLAB的第(6)天
TIA botu - basic operation
VIM uses vundle to install the code completion plug-in (youcompleteme)
Meaning and usage of volatile
Method 2 of drawing ROC curve in R language: proc package
面试题 17.10. 主要元素
Website pressure measurement tools Apache AB, webbench, Apache jemeter
Spark 算子之groupBy使用