def fib(n):
if n <= 4:
return 1
else:
return fib(n-1) + fib(n-2)
print("一年后共繁殖{}对兔子".format(fib(12)))
s = input("请输入一个字符串:")
if not s:
print("请不要输入空字符串!")
s = input("请重新输入一个字符串:")
a = reversed(list(s))
if list(a) == list(s):
print("该字符串是回文")
else:
print("该字符串不是回文")
def zhishu(n):
#判断是否是质数
for i in range(2, n // 2 + 1):
if n % i == 0:
return False
return True
n = int(input("输入你想要的正整数:"))
num = 0
for i in range(2, n):
if zhishu(i):
num += i
print(f"质数的总和:{num}")
a = int(input("请输入所相加的数字:"))
b = int(input("请输入相加的次数:"))
temp = ""
res = 0
for i in range(b):
temp = temp + "1"
res = res + a * int(temp)
print(res)
a = [1,2,3,8,9,10]
b = [2,6,8,14,50,60]
def merge2(a, b):
la,lb = len(a), len(b)
c = []
i = j = 0
while i
结果:
#给定一个非负整数数组A,将该数组中的所有偶数都放在奇数元素之前
def even_before_odd(ls=[]):
'''
将一个非负整数数组中的所有偶数都放在奇数元素之前
'''
for i in range(len(ls)):
if ls[i] % 2 != 0:
for j in range(i + 1, len(ls)):
if ls[j] % 2 == 0:
ls[i], ls[j] = ls[j], ls[i]
break
return ls
ls = [1, 2, 3, 4, 5, 6, 7, 8, 9]
print(f'原列表为:{ls}')
print(f'将偶数置于奇数前,列表变为:{even_before_odd(ls)}')
[1,3,1,2,2,8,8]
def notpair(ls):
'''
找出不成对的元素
'''
temp = 0
for i in ls:
temp ^= i
return temp
ls = [1, 4, 1, 3, 3, 8, 9, 8, 9]
print(f'{ls}中不成对的元素为:{notpair(ls)}')