Practice is an indispensable exercise in programming. Let’s take a look at recursive questions and function-related questions.
1. There is a pair of rabbits. From the 3rd month after birth, a pair of rabbits will be born every month, and the
little rabbit will give birth to a pair of rabbits every month after the 3rd month.
If the rabbits are not dead, ask the nth What is the logarithm of the rabbit in the month?
'''
作者:lsf
time:2021.1.19
有一对兔子,从出生后的第3个月起,每个月都生一对兔子,
小兔子长到第3个月后每个月又生一对兔子,
假如兔子不死,问第n个月第兔子对数为多少?
f(n)= f(n-1)+f(n-2) {n 代表月份}
'''
def rabit_twain(time):
if time==1 or time==2:
return 1
else:
month=rabit_twain(time-1)+rabit_twain(time-2)
return month
print(rabit_twain(4))
2. Complete a blog system (requires users to register, log in, publish blogs, delete blogs, add blogs, and query blog functions)
'''
作者:lsf
time:2021.1.19
'''
import sys
def ring_up():
users = []
while True:
print("欢迎登录我们的用户管理系统")
print("\t\t 1、用户注册")
print("\t\t 2、用户登录")
print("\t\t 3,进入博客")
print("\t\t 4、退出系统")
choice = input("请选择您的操作:")
if choice == "1":
while True:
print("欢迎进入到用户注册页面")
username = input("请输入用户名称:")
password = input("请输入用户密码:")
confirm_password = input("请再次确认密码:")
# 数据校验
# 用户名称不能为空
if username == None or len(username) == 0:
print("对不起,用户为空")
continue
# 用户名称不能重复
flag = False
for u in users:
if u.get("username") == username:
print("对不起,该用户已经存在,请重新注册")
flag = True
break
if flag:
continue
# 密码长度不能少于3位
if len(password) < 3:
print("对不起,密码长度不能少于3位")
continue
# 两次密码不一致
if password != confirm_password:
print("两次密码不一致")
continue
# 最后如何保存用户名称和密码
user = {"username": username, "password": password}
users.append(user)
print("用户注册成功,请登录")
break
elif choice == "2":
print("欢迎进入到用户登录页面")
username = input("请输入用户名称:")
password = input("请输入用户密码:")
# 判断用户名和密码是否存在于users
for user in users:
if user["username"] == username and user.get("password") == password:
print("恭喜您,{},登录成功".format(username))
else:
print("对不起,登录失败,请重新登录~~")
elif choice == "3":
blong = []
print("登陆成功,您可以选择以下操作")
print("1,添加博客")
print("2,查询博客")
print("3,删除博客")
print("4,退出系统")
choice = int(input("请选择您的操作:"))
if choice == 1:
print("欢迎进入添加博客页面")
content = input("请输添加内容:")
prople = int(input("1,发表博客,2取消发表"))
if prople == 1:
blong.append(content)
print("添加博客成功")
continue
else:
print("取消发表成功")
continue
elif choice == 2:
print(blong)
continue
elif choice ==3:
blong.clear()
print("删除成功")
continue
else:
sys.exit()
else:
sys.exit()
ring_up()
3. Assuming that the user can only go up to one step or two steps each time, please write a function to determine how many ways the user has to go to the nth step
'''
作者:lsf
time:2021.1.19
f(n)= f(n-1)+f(n-2)
'''
def stairs(step):
if step == 1 or step == 2:
return 1
else:
month =stairs(step - 1) +step(step - 2)
return month
print((4))
4. Find the number within 10000 that can be divisible by 5 or 6, but not divisible by both at the same time (function)
'''
作者:lsf
time:2021.1.19
f(x)=f(x-5)f(x-6)
'''
import sys
sys.setrecursionlimit(100000)
def num(n):
while n >= 0:
if n%5==0 or n%6==0 and n%30 != 0 :
print(n)
return num(n-1)
else:
return num(n-1)
print(num(1000))
5. Write a method to calculate the sum of all even subscript elements of the list (note the return value)
'''
作者:lsf
time:2021.1.19
'''
ls=[1,2,3,4,5,6,7,8,9,10,23,14,66]
l=[]
def even_number(lis):
for i in lis:
if i % 2 == 0:
l.append(i)
return sum(l)
print(even_number(ls))
6. Determine whether a number is prime (prime number)? How to declare a method?
'''
作者:lsf
time:2021.1.19
能不能被整除
'''
num=int(input("请输入一个整数:"))
def is_prime(num):
for i in range(2,num//2+1):
if num % i == 0 :
return print("%d不是质数"%num)
else:
return print("%d是质数"%num)
is_prime(num)
7. Please use the function to complete the three sorts of selection, bubbling, and insertion
https://blog.csdn.net/weixin_47514459/article/details/112791073
8. A person enters the following chessboard, asks to start from the upper left corner, and finally comes out from the lower right corner (required to only move forward, not back),
question: How many moves are there?
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
'''
作者:lsf
time:2021.1.19
'''
def get_count(board):
# 求棋盘横向的宽度
width = len(board[0])
# 求纵向的高度
height = len(board)
if width == 0 and height == 0:
return 0
# 棋盘只有一个位置,也就是只有一种走法
if width == 1 and height == 1:
return 1
# 将横向第一行设置为0
for i in range(width):
board[0][i] = 1
for i in range(height):
board[i][0] = 1
for i in range(1, height):
for j in range(1, width):
board[i][j] = board[i][j - 1] + board[i - 1][j]
return board[height - 1][width - 1]
b = [[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0]]
print(get_count(b))