目录
1、求一个十进制的数值的二进制的0、1的个数
2、实现一个用户管理系统(要求使用容器保存数据) [{name: xxx, pass: xxx, ……},{},{}]
3、求1~100之间不能被3整除的数之和
4、给定一个正整数N,找出1到N(含)之间所有质数的总和
5、计算PI(公式如下:PI=4(1-1/3+1/5-1/7+1/9-1.......)
6、给定一个10个元素的列表,请完成排序(注意,不要使用系统api)
7、求 a+aa+aaa+.......+aaaaaaaaa=?其中a为1至9之中的一个数,项数也要可以指定
8、合并两个有序数组,合并后还是有序列表
9、给定一个非负整数数组A,将该数组中的所有偶数都放在奇数元素之前
def ten_to_two(num):
binary_num = bin(num)
ls = list(str(binary_num))[2:]
count_0 = 0
count_1 = 0
for i in ls:
if int(i) == 0:
count_0 += 1
else:
count_1 += 1
return count_0,count_1
num = int(input('请输入一个整数:'))
print(f'将{num}转换为二进制为:{bin(num)[2:]}')
print(f'其中0的数量为:{ten_to_two(num)[0]}个')
print(f'其中1的数量为:{ten_to_two(num)[1]}个')
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()
a = 0
for x in range(1,101):
if x % 3 == 0:
continue
else:
a = x + a
print(f"百位以内不可被三整除的数和为:{a}")
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}')
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()
l = eval(input("请输入一个列表:"))
ls=list(l)
ls.sort()
s=set(ls)
print(s)
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)}')
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)}')
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)}')