P105 计算圆周率——无穷级数法
def calculate_pi(x): #定义莱布尼茨级数公式
pi = 0
i = 1
t = 0
while 1/i >= x:
term = (-1) ** t / i
pi += term
i = i + 2
t += 1
return pi * 4
x = float(input()) #输入浮点数
result = calculate_pi(x)
print("%.8f" % result)
P106 扑克牌游戏
import copy
import random
list1 = ['♠', '♥', '♣', '♦']
list2 = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A']
New_List = []
PlayerList = {}
SortPlayerList = {}
num = int(input())
seed = int(input())
for i in list1:
New_List = New_List + [i + item for item in list2]
New_List.append('jokers')
New_List.append('JOKERS')
List = copy.deepcopy(New_List)
print("参与游戏的人数:" + str(num))
print("新牌顺序")
print(*New_List, sep=' ')
random.seed(seed)
random.shuffle(New_List)
print("洗牌顺序")
print(*New_List, sep=' ')
print("每个人手上分到的牌")
for i in range(0, num):
PlayerList[i] = New_List[i::num]
print(*PlayerList[i], sep=' ')
print("每个人手上排序的牌")
for n in range(0, num):
Temp_List = []
for i in List:
for j in PlayerList[n]:
if i == j:
Temp_List.append(i)
SortPlayerList[n] = Temp_List
print(*SortPlayerList[n], sep=' ')
P107 凯撒密码
txt = input()
pwd = "" # 初始化一个空字符串pwd,用于存放加密后的字符
for ch in txt:
if ch.isupper():# 使用ord函数获取ch的ASCII码,然后减去大写字母A的ASCII码,再加上3,再对26取模
mw = chr(((ord(ch) - ord("A") + 3) % 26 + ord("A")))
elif ch.islower():
mw = chr(((ord(ch) - ord("a") + 3) % 26 + ord("a")))
elif ch.isdigit():
mw = chr(((ord(ch) - ord("0") + 3) % 26 + ord("0")))
else:
mw = ch
pwd = pwd + mw # 将mw拼接到pwd字符串的末尾
print(pwd)
P108 素数筛选
# 素数判定
def is_prime(n):
if n == 1:
return False
for i in range(2, int(n**0.5) + 1):
if n % i == 0:
return False
return True
# 读取输入的正整数列表
numbers = list(map(int, input("").split()))
# 筛选素数
primes = [num for num in numbers if is_prime(num)]
#输出筛选出的素数
print(" ".join(map(str, primes)))
P109 日期计算
run = [0,31,29,31,30,31,30,31,31,30,31,30,31]#闰年
ping = [0,31,28,31,30,31,30,31,31,30,31,30,31]#平年
day = 0
str = input("")
y,m,d = str.split("-")#分割,转换成列表
if (int(y) % 400 ==0 ) or (int(y) % 4 ==0 and int(y) % 100 != 0):
for i in range(int(m)):
day = day + run[i]
day = day + int(d)
else:
for i in range(int(m)):
day = day + ping[i]
day = day + int(d)
print(day)