wangxinniankuaile @ 2024-11-13 20:55:10
numbers = int(input()) #输入灯数number同时有多少个灯代表需要操作多少次
list_number = []
for i in range(numbers): #开始状态,将每个灯开启 设开启状态为1,关闭状态为0
list_number.append(1)
if numbers == 1: #有1盏灯的情况
list_number = [0]
elif numbers == 2: #有2盏灯的情况
list_number = [0,1]
elif numbers == 3: #有3盏灯的情况
list_number = [0,1,0]
elif numbers >= 4: #有4盏灯及其以上的情况,操作number次
for i in range(numbers):
if i+1 == 1: # 第一次操作将所有灯关闭
for j in range(numbers):
list_number[j] = 0
#print(list_number)
if (i+1) % 2 == 0: # 第二次操作将所有2的倍数的打开
list_number[i] = 1
if i+1 >= 3: # 第三次以上的操作将所有是i倍数序号的灯取相反
for k in range(3,numbers+1):
if (i+1) % k == 0:
if list_number[i] == 0:
list_number[i] = 1
else:
list_number[i] = 0
#print(list_number)
for idex, item in enumerate(list_number):
if item == 0:
print(idex+1,end=" ") # 列表索引值从0开始,而灯是从1开始,所以灯的序号为索引值加1
by SunXiaolang @ 2024-11-13 20:57:48
@wangxinniankuaile 其实只需要判断是不是平方数就好
by Estelle_N @ 2024-11-13 21:06:38
@wangxinniankuaile
by wangxinniankuaile @ 2024-11-13 21:23:39
elif numbers >= 4: #有4盏灯及其以上的情况,操作number次
for i in range(numbers):
if i+1 == 1: # 第一次操作将所有灯关闭
for j in range(numbers):
list_number[j] = 0
#print(list_number)
if (i+1) % 2 == 0: # 第二次操作将所有2的倍数的打开
list_number[i] = 1
if i+1 >= 3: # 第三次以上的操作将所有是i倍数序号的灯取相反
for k in range(3,numbers+1):
if (i+1) % k == 0:
if list_number[i] == 0:
list_number[i] = 1
else:
list_number[i] = 0
by wangxinniankuaile @ 2024-11-13 21:25:53
@Estelle_N 是N个人,上面一段代码就是n次操作,从3后操作都一样,所以就放在一起了
if i+1 >= 3: # 第三次以上的操作将所有是i倍数序号的灯取相反
for k in range(3,numbers+1):
by Estelle_N @ 2024-11-14 10:48:49
@wangxinniankuaile
抱歉,没有仔细看。
最开始 append
一个
numbers = int(input()) #输入灯数number同时有多少个灯代表需要操作多少次
list_number = []
list_number.append(0)
for i in range(numbers): #开始状态,将每个灯开启 设开启状态为1,关闭状态为0
list_number.append(1)
if numbers == 1: #有1盏灯的情况
list_number = [0]
elif numbers == 2: #有2盏灯的情况
list_number = [0,1]
elif numbers == 3: #有3盏灯的情况
list_number = [0,1,0]
elif numbers >= 4: #有4盏灯及其以上的情况,操作number次
for i in range(numbers):
if i+1 == 1: # 第一次操作将所有灯关闭
for j in range(numbers):
list_number[j] = 0
#print(list_number)
if (i+1) % 2 == 0: # 第二次操作将所有2的倍数的打开
list_number[i] = 1
if i+1 >= 3: # 第三次以上的操作将所有是i倍数序号的灯取相反
for k in range(3,numbers+1):
if (i+1) % k == 0:
if list_number[i] == 0:
list_number[i] = 1
else:
list_number[i] = 0
#print(list_number)
for idex, item in enumerate(list_number):
if item == 0:
print(idex+1,end=" ") # 列表索引值从0开始,而灯是从
by Estelle_N @ 2024-11-14 10:49:30
其实不需要那么多特判。
numbers = int(input())
list_number = []
list_number.append(0)
for i in range(numbers):
list_number.append(1)
for i in range(1, numbers + 1):
for j in range(1, numbers + 1):
if j % i == 0:
list_number[j] ^= 1
for i in range(1, numbers + 1):
if not list_number[i]:
print("%d" % i, end = ' ')
print()