当前位置:网站首页>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
边栏推荐
- Force buckle-746 Climb stairs with minimum cost
- Spark 算子之sortBy使用
- Algorithem_ ReverseLinkedList
- leetcode-396 旋转函数
- 通过Feign在服务之间传递header请求头信息
- 浅谈 NFT项目的价值、破发、收割之争
- Gartner predicts that the scale of cloud migration will increase significantly; What are the advantages of cloud migration?
- Metalife established a strategic partnership with ESTV and appointed its CEO Eric Yoon as a consultant
- Day (3) of picking up matlab
- [open source tool sharing] MCU debugging assistant (oscillograph / modification / log) - linkscope
猜你喜欢

5 minutes, turn your excel into an online database, the magic cube net table Excel database

Spark 算子之distinct使用

Using JSON server to create server requests locally

Spark 算子之sortBy使用

Jour (9) de ramassage de MATLAB

Distinct use of spark operator

Day (4) of picking up matlab

运维流程有多重要,听说一年能省下200万?

Spark 算子之groupBy使用

Day (8) of picking up matlab
随机推荐
实现缺省页面
Day (5) of picking up matlab
Matplotlib tutorial 05 --- operating images
Sortby use of spark operator
Website pressure measurement tools Apache AB, webbench, Apache jemeter
【第5节 if和for】
MySQL - execution process of MySQL query statement
[section 5 if and for]
Force buckle-746 Climb stairs with minimum cost
ESP32编译环境的搭建
Application case of GPS Beidou high precision satellite time synchronization system
Day (2) of picking up matlab
多线程原理和常用方法以及Thread和Runnable的区别
保姆级Anaconda安装教程
How important is the operation and maintenance process? I heard it can save 2 million a year?
Using JSON server to create server requests locally
Construction of esp32 compilation environment
shell_2
TIA博图——基本操作
Day (3) of picking up matlab