1. 求一个十进制的数值的二进制的0、1的个数
def er(x):
a = bin(x)
b = str(a).count("1")
c = str(a).count("0") - 1
print(f"{a},count 1:{b},count 0:{c}")
x = int(input("enter a number:"))
er(x)
2. 实现一个用户管理系统(要求使用容器保存数据)
[{name: xxx, pass: xxx, ……},{},{}]
users = []
while True:
print("\t\t欢迎登录用户管理系统")
print("\t\t 1、用户注册")
print("\t\t 2、用户登录")
print("\t\t 3、退出系统")
choice = input("请输入您的选择:")
if choice == "1":
while True:
username = input("请输入用户名:")
password = input("请输入密码:")
password_again = 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
if password != password_again:
print("两次密码不一致")
continue
user = {"username": username, "password": password}
users.append(user)
print("用户注册成功,请登录")
print("\n")
break
elif choice == "2":
print("\n")
username = input("请输入用户名:")
password = input("请输入密码:")
for user in users:
if user["username"] == username and user.get("password") == password:
print(f"尊敬的用户{username},欢迎回来")
else:
print("对不起,登录失败,请重新登录")
else:
sys.exit()
3. 求1~100之间不能被3整除的数之和
a = 0
for x in range(1,101):
if x % 3 == 0:
continue
else:
a = x + a
print(f"百位以内不可被三整除的数和为:{a}")
4. 给定一个正整数N,找出1到N(含)之间所有质数的总和
def sum(x):
if x == 1:
return False
for i in range(2,x//2 + 1):
if x % i == 0:
return False
return True
N = int(input('请输入一个正整数N:'))
sum_N = 0
for x in range(1,N+1):
if sum(x):
sum_N += x
print(f'1到N(含)之间所有质数的总和为:{sum_N}')
5. 计算PI(公式如下:PI=4(1-1/3+1/5-1/7+1/9-1.......)
def PI():
a = 0
b = 0
for i in range(1,99999999,4):
a += 4 * (1 / i)
for i in range(3,99999999,4):
b -= 4 * (1 / i)
print(a + b)
PI()
6. 给定一个10个元素的列表,请完成排序(注意,不要使用系统api)
l = eval(input("请输入一个列表:"))
ls=list(l)
ls.sort()
s=set(ls)
print(s)
7. 求 a+aa+aaa+.......+aaaaaaaaa=?其中a为1至9之中的一个数,项数也要可以指定。
def sum(a,n):
sum_a = 0
for i in range(1,n+1):
num = int(f'{a}'*i)
sum_a += num
return sum_a
a = int(input('请输入一个在区间[1,9]的正整数:'))
n = int(input('请输入指定的项数:'))
print(f'所求多项式的和为:{sum(a,n)}')
8. 合并两个有序数组,合并后还是有序列表
def selection_sort(ls=[]):
for i in range(len(ls)):
index_min = i
for j in range(i+1,len(ls)):
if ls[index_min] > ls[j]:
index_min = j
ls[index_min],ls[i] = ls[i],ls[index_min]
return ls
l1 = eval(input("请输入一个列表:"))
l2 = eval(input("请输入一个列表:"))
l1.extend(l2)
print(f'合并后的有序列表为:{selection_sort(l1)}')
9. 给定一个非负整数数组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 = eval(input("请输入一个列表:"))
print(f'将偶数置于奇数前,列表变为:{even_before_odd(ls)}')
10. 给定一个包含n+1个整数的数组nums,其数字在1到n之间(包含1和n),可知至少存在一个重复的整数,假设只有一个重复的整数,请找出这个重复的数
nums = [1,3,4,2,2]
repeatDict = {}
for num in nums:
if num not in repeatDict:
repeatDict[num] = 1
else:
print(num)
nums.sort()
pre = nums[0]
n = len(nums)
for index in range(1, n):
if pre == nums[index]:
print (pre)
pre = nums[index]
nums.sort()
for i in range(len(nums)-1):
if nums[i] == nums[i+1]:
print(nums[i])
11. 找出10000以内能被5或6整除,但不能被两者同时整除的数(函数)
def math_5_6(x):
count=[]
for i in range(1,x+1):
if (i % 5 == 0 or i % 6 ==0 ):
if i % 5 == 0 and i % 6 ==0:
continue
else:
count.append(i)
print(count)
math_5_6(10000)
3. 写一个方法,计算列表所有偶数下标元素的和(注意返回值)
def list_sum(ls):
sum = 0
i = 0
while i < len(ls):
sum += ls[i]
i += 2
return sum
ls = [1,2,3,4,5,6,7,8,9]
print(f"列表是:{ls},偶数下标元素和是:{list_sum(ls)}")
4. 【选做】某个人进入如下一个棋盘中,要求从左上角开始走, 最后从右下角出来(要求只能前进,不能后退),问题:共有多少种走法?
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
def uniquePaths(m, n):
dp = [[0] * n for _ in range(m)]
for i in range(m):
dp[i][0] = 1
for j in range(n):
dp[0][j] = 1
for i in range(1, m):
for j in range(1, n):
dp[i][j] = dp[i-1][j] + dp[i][j-1]
return dp[m-1][n-1]
m = 8
n = 5
result = uniquePaths(m, n)
print("共有", result, "种走法")
5. 【选做】汉诺塔
def rabbit(month):
if month <= 2:
return 2
else:
return rabbit(month - 1) + rabbit(month - 2)
if __name__ == "__main__":
month = int(input("请输入month:"))
for i in range(0, month):
print(rabbit(i))