求一个十进制的数值的二进制的0、1的个数
a = int(input("请输入一个十进制数:"))
m = ''
s = []
q = 0
p = 0
type(m)
while a > 0 :
i = a % 2
s.append(i)
a = a // 2
print(f"长度{len(s)}")
for j in s:
if s[j] == 1:
p += 1
else :
q += 1
while len(s) > 0:
m += str(s.pop())
print(f"二进制{m}")
print(f"一的个数{p},零的个数{q}")
求1~100之间不能被3整除的数之和 4
def integer(n):
j = 0
for i in range(1,n+1):
if i % 3 != 0:
j += i
return j
print(integer(100))
给定一个正整数N,找出1到N(含)之间所有质数的总和
def primeNUM(N):
x = 0
if N==1:
print('')
N += 1
for i in range(2, N+1):
for j in range(2, i + 1):
if i % j == 0:
break
if j == i:
x += i
print(x)
primeNUM(100)
计算PI(公式如下:PI=4(1-1/3+1/5-1/7+1/9-1.......)
# 计算PI(公式如下:PI=4(1-1/3+1/5-1/7+1/9-1.......)
def number_1(n):
PI = 0
for i in range(1 , n + 1 ):
a = (- 1 ) ** (i - 1)
b = 2 * i - 1
c = ( a / b )
PI += c
return(4 * PI)
print(number_1(10000000))
给定一个10个元素的列表,请完成排序
def list_1(acc):
for i in range(len(acc) - 1):
for j in range (len(acc)-1-i):
if acc[j] > acc[j + 1]:
acc[j],acc[j + 1] = acc[j + 1],acc[j]
return acc
ls = [6,5,7,8,9,1,0,3,4,7,8,0,2,10]
print(list_1(ls))
求 a+aa+aaa+.......+aaaaaaaaa=?其中a为1至9之中的一个数,项数也要可以指定。
# 求 a+aa+aaa+.......+aaaaaaaaa=?其中a为1至9之中的一个数,项数也要可以指定。
def number_1(n,N):
x = 0
for i in range(1,N + 1):
s = n ** i
x += s
return x
print(number_1(5,3))
合并两个有序数组,合并后还是有序列表
def loop_merge_sort(l1,l2):
tmp = []
while len(l1)>0 and len(l2)>0:
if l1[0] <l2[0]:
tmp.append(l1[0])
del l1[0]
else:
tmp.append(l2[0])
del l2[0]
while len(l1)>0:
tmp.append(l1[0])
del l1[0]
while len(l2)>0:
tmp.append(l2[0])
del l2[0]
return tmp
L1=[23,45,67,77,78,80,90]
L2=[4,11,26,33,42,61,80]
print(loop_merge_sort(L1,L2))
给定一个非负整数数组A,将该数组中的所有偶数都放在奇数元素之前
a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
b = []
c = []
for i in a:
if i % 2 == 0:
b.append(i)
else:
c.append(i)
b.extend(c)
print(b)
给定一个包含n+1个整数的数组nums,其数字在1到n之间(包含1和n), 可知至少存在一个重复的整数,假设只有一个重复的整数,请找出这个重复的数
def sum():
print("10000以内能被5或6整除的数为:")
for i in range(1,10000):
if i % 30 != 0:
if i % 5==0 or i % 6==0:
print(i,end = " ")
sum()
写一个方法,计算列表所有偶数下标元素的和(注意返回值)
def sum_even(ls):
sum_even = 0
for i in range(0,len(ls),2):
sum_even += ls[i]
return sum_even
ls = [1,2,3,4,5,6,7,8,9,10,11,12]
print(f'{ls}中所有偶数下标元素的和为:{sum_even(ls)}')
实现一个用户管理系统(要求使用容器保存数据) [{name: xxx, pass: xxx, ……},{},{}]
Account = [{'Name':'Jack','Password':'123456'},
{'Name':'Nancy','Password':'234567'},
{'Name':'Mandy','Password':'345678'},
{'Name':'Kimmy','Password':'456789'}]
def home_page():
print("\t英雄联盟商城登录界面")
print("~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~")
print("\t\t1. 用户登录")
print("\t\t2. 新用户注册")
print("\t\t3. 退出系统")
print("~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~")
def option_page():
print("\t英雄联盟商城首页")
print("~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~")
print("\t\t1. 进入英雄超市")
print("\t\t2. 休闲小游戏")
print("\t\t3. 退出登录")
print("~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~")
def name():
global username
global password
global list1_username
global list2_password
list1_username = []
list2_password = []
for j in range(len(Account)):
all_username = Account[j]['Name']
list1_username.append(all_username)
all_password = Account[j]['Password']
list2_password.append(all_password)
print(home_page())
choick=input('请输入你的选择')
if choick == '1' :
name()
username = input('请输入您的用户名:')
password = input('请输入6位用户密码:')
for i in range(len(Account)):
if username in list1_username and password in list2_password:
if username == Account[i]['Name']:
if password == Account[i]['Password']:
print(option_page())
break
else:
print('用户名和密码不匹配,请重新输入!')
elif username == '' and password == '': # 密码和用户输入为空,执行
print('用户名和密码不能为空,请重新输入!')
break
elif choick == '2' :
name()
new_username = input('请输入您的用户名:')
if new_username in list1_username:
print('用户名已存在,请重新输入!')
elif new_username == '':
print('用户名不能为空,请重新输入!')
else:
new_user = {'Name': new_username}
Account.append(new_user)
new1_password = input('请输入您的6位数字密码:')
new2_password = input('请再次输入您的6位数字密码:')
if new1_password == '' and new2_password == '':
print('密码不能为空,请重新输入!')
elif new1_password == new2_password:
if len(new2_password) == 6 and new2_password.isdigit():
new_user['Password'] = new2_password
print('***恭喜您,新账户注册成功!***')
elif len(new2_password) != 6:
print('密码长度不正确,请重新输入!')
elif len(new2_password) == 6 or new2_password.isdigit():
print('密码不是纯数字密码,请重新输入')
elif new1_password != new2_password:
print('两次输入密码不一致,请重新输入!')
elif choick == '3' :
sys.exit()