1.有一堆硬币,每次只能拿一个或者两个,求最少多少次可以拿完硬币
[10, 8, 5, 3, 27, 99]
num = 0
sl = [10, 8, 5, 3, 27, 99]
for i in sl:
a = i % 2
if a == 0:
num = i // 2 + num
else:
num = i // 2 + num + 1
print("需要取的次数是",(num))
2.如果两个素数之差为2,这样的两个素数就叫作"孪生数",找出100以内的所有"孪生数".
num = []
for i in range(2, 101):
flag = True
for j in range(2, i):
if i % j == 0:
flag = False
break
if flag:
num.append(i)
index = 0
print(num)
while index < len(num) - 1:
first = num[index]
second = num[index + 1]
if second - first == 2:
print(f"{first}和{second}之间是孪生素数")
index += 1
3.给定一个列表,求最大值(不能使用系统api),求最小值,求平均值、求和
list = [1,2,3,4,5,6,7,8]
print("列表",list)
num_max = list[0]
num_min = list[0]
num_and = 0
for i in range(1,len(list)):
if num_max < list[i]:
num_max = list[i]
elif num_min > list[i]:
num_min = list[i]
else:
pass
print("最大值:",num_max)
print("最小值:",num_min)
for i in list:
num_and += i
print("和:",num_and)
num_average = num_and/len(list)
print("平均值:",num_average)
4.将list中的重复数据去重,至少使用两种方案两个列表进行合并操作
a = [1,2,3,1,2,8,9,5]
print("去重前:",a)
b = list(set(a))
print("去重后:",b)
j = []
for i in a:
if i not in j:
j.append(i)
print("去重后:",j)
5.使用列表判断一个列表是否在另外一个列表中列表的反转 列表的排序
a = [1,2,3,6,8,9,10,11,12]
b = [1,2,3]
j = list(set(a).intersection(set(b)))
print(j)
6.如何将0-10随机存入列表中
import random
ls = []
for i in range(10):
ls.append(random.randint(0, 10))
print(ls)
7.存在一个1~n的连续列表,在其中不小心重复了一个值,请找出这个重复值
[1,2,3,4,5]
res = 0
arr = [1, 4, 2, 3, 4, 5]
for i in range(0, len(arr)):
res ^= i ^ arr[i]
print(res)