目录
A、a
B、¥a
C、_12
D、$a@12
E、false
F、False
答案BDF
2、输入数,判断这个数是否是质数(要求使用函数 + for循环)
a = int (input("请输入一个数:"))
def qwe(num):
for x in range(2,num):
if num % x == 0:
print(f"{num}是合数")
break
else:
print(f"{num}是质数")
break
qwe(a)
def qwe(num):
for x in range(2,num):
if num % x == 0:
break
else:
print(num)
for i in range(50,151):
qwe(i)
def sxh():
for i in range(100,1000):
a = i // 100
b = i % 100 // 10
c = i % 10
if i == a ** 3 + b ** 3 + c ** 3:
print(i)
sxh()
5、验证:任意一个大于9的整数减去它的各位数字之和所得的差,一定能被9整除
a = input("请输入一个大于九的数:")
b = []
c = 0
d = 0
for i in range(0,len(a)):
b.append(a[i]) #把字符串切片存储到列表中
for i in range(0,len(b)):
d = d + int(b[i])
c = int(a) - d #计算出各位数字之和
if i // 9 == 0:
print("猜想正确")
else:
print("猜想错误")
6、一个五位数,若在它的后面写上一个7,得到一个六位数A,,若在它前面写上一个7,得到一个六位数B,B是A的五倍,求此五位数
def fix():
for i in range(10000,100000):
a = i * 10 + 7
b = 700000 + i
if b // a == 5 and b % a == 0:
print(i)
fix()
7、有一堆硬币,每次只能拿一个或者两个,求最少多少次可以拿完硬币 [10, 8, 5, 3, 27, 99]
def coin():
x = 0
if i % 2 == 0:
x = i / 2
else:
x = (i + 1) / 2
print(x)
a = [10,8,5,3,27,99]
for i in a:
coin()
8、如果两个素数之差为2,这样的两个素数就叫作"孪生数",找出100以内的所有"孪生数"
def twin():
t = []
for a in range(2,101):
flag = True
for x in range(2,a):
if a % x == 0:
flag = False
break
if flag:
t.append(a)
else:
pass
i = 1
while i < len(t):
if t[i] - t[i - 1] == 2:
print(f"{t[i]}和{t[i - 1]}为孪生素数")
i += 1
twin()
9、给定一个列表,求最大值(不能使用系统api),求最小值,求平均值、求和
def list():
a = [0,1,2,3,4,5,6]
i = 0
x = 0
y = 0
z = 0
v = 0
while i < len(a):
x += a[i]
if a[i] > y:
y = a[i]
elif a[i] < z:
z = a[i]
i += 1
v = x / len(a)
print(f"max:{y},min:{z},sum:{x},avg:{v}")
list()
a = [1,2,2,3,3,4,5,5,6,7,8,8,9]
b = list(set(a))
c = []
for i in a:
if i not in c:
c.append(i)
print(f"原list为:{a}")
print(f"去重后为:{b}")
print(f"去重后为:{c}")
a = [1,2,3,4,5]
b = [6,7,8,9,10]
c = a + b
print(c)
a = [1,2,3,4,5,6,7]
b = [1,3,5,7]
c = [2,4,6,8]
print(set(b).issubset(set(a)))
print(set(c).issubset(set(a)))
a = [1,3,5,7,9,2,4,6,8,10]
b = list(reversed(a))
print(f"反转后为:{b}")
c = list(sorted(a))
print(f"排序后为:{c}")
import random
a = []
for i in range(10):
a.append(random.randint(0,10))
print(a)