目录
第1关 回文素
def isprime(n):
for j in range(2, int(n**(1/2)) + 1):
if n % j == 0:
return 0
return 1
def plalindrome_prime(n):
n1 = n[::-1]
if n1 == n:
return 1
return 0
x = int(input())
i = 0
n = 2
while (i < x):
if (isprime(n) and plalindrome_prime(str(n))):
print(n, end=' ')
i += 1
n += 1
第2关 反素数
def is_prime(n):
for i in range(2,int(n**(1/2)+1)+1):
if n % i ==0:
return False
return True
def reverse_prime(n):
if is_prime(n) and is_prime(int(str(n)[::-1])) and str(n) !=str(n)[::-1]:
return True
else:
return False
a = int(input())
times = 0
n = 2
while times < a:
if reverse_prime(n):
print(n,end = " ")
times += 1
n +=1
第3关 哥德巴赫猜想
def is_prime(n):
"""判断素数的函数,接收一个正整数为参数,参数是素数时返回True,否则返回False"""
# ====================Begin===================================
# 补充你的代码
if n < 2:
return False
elif n == 2:
return True
else:
a = int(n ** (1 / 2) + 1)
for i in range(2, a + 1):
if n % i == 0:
return False
else:
return True
# ======================End=================================
def goldbach_conjecture(num):
""" 哥德巴赫猜想, 接收一个不小于4的正整数为参数。
当参数为不小于4的偶数时,将其分解为两个素数的加和,按小数+数的格式输出。
有多种组合时全部输出,但不输出重复的组合,例如输出8=3+5,不输出8=5+3。
参数为奇数或小于4时,输出'Data error!'
"""
# ====================Begin===================================
# 补充你的代码
if 4<=num and num % 2 == 0:
for i in range(1,num + 1):
n = num - i
if is_prime(i) and is_prime(n) and i<=n :
print(f"{num}={i}+{n}")
else:
print('Data error!')
# ======================End=================================
if __name__ == '__main__':
positive_even = int(input()) # 输入一个正数
goldbach_conjecture(positive_even)
第4关 货币转换
a = input()
b = eval(input())
if '$' in a and b>0:
c = eval(a.replace('$',""))
print(f"{c*b:.2f}¥")
elif "¥" in a and b>0:
c = eval(a.replace('¥', ""))
print(f"{c/b:.2f}$")
else:
print("Data error!")
第5关 个人信息提取
a = list(map(str,input().split()))
b = []
c=0
for i in a[4]:
if str.isdigit(i) and c <4:
b.append(i)
c+=1
b = ''.join(b)
print("姓名:" + a[1])
print("班级:" + a[2])
print("出生:" + str(b)+"年")
第6关 身份证号基本信息
a = input()
year = a[6:10]
month = a[10:12]
date = a[12:14]
if int(a[16])%2 == 0:
print(f'出生:{year}年{month}月{date}日')
print('性别:女')
else:
print(f'出生:{year}年{month}月{date}日')
print('性别:男')
第7关 各位数字之和为5的数
a = int(input())
for i in range(a+1):
s = 0
for n in str(i):
s = s + int(n)
if s == 5:
print(i, end=" ")
第8关 字符串长度
a = input()
print(len(a))
第9关 字符串加密
import string
def caesar_cipher(text):
lower = string.ascii_lowercase
upper = string.ascii_uppercase
digit = string.digits
before = lower + upper + digit
after = lower[3:]+lower[:3] + upper[5:]+upper[:5]+ digit
result = ''.maketrans(before, after)
return text.translate(result)
if __name__ == '__main__':
plaintext = input()
print(caesar_cipher(plaintext))
第10关 输出单词
a = input()
b = a.split()
for i in b:
print(i)
第11关 字符大小写转换
import string
def caesar_cipher(text):
lower = string.ascii_lowercase
upper = string.ascii_uppercase
digit = string.digits
before = lower + upper + digit
after = upper + lower+ digit
result = ''.maketrans(before, after)
return text.translate(result)
if __name__ == '__main__':
plaintext = input()
print(caesar_cipher(plaintext))
第12关 查找指定字符
a = input()
b = input()
if a in b:
print(f'index = {b.index(a)}')
else:
print("Not Found")
第13关 随机密码生成器
import random
n,s= map(int,input().split(','))
random.seed(s)
i = 0
while i < n:
i += 1
print(random.choice('0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&\()*+,-.'),end = "")
第14关 模拟生成微软序列号
import random
n = int(input())
x = int(input())
random.seed(x)
def wrxl():
list = []
times = 0
while times < 5:
for i in range(5):
a = random.choice("BCEFGHJKMPQRTVWXY2346789")
list.append(a)
list.append('-')
list1 = map(str,list)
times += 1
list2 = ''.join(list1)
list2 = list2.strip('-')
return list2
for i in range(n):
print(wrxl())
第15关 数列求和
A = input()
N = int(input())
if 0 <= N and 1<= int(A) <=9 :
def slqh(A,N):
list = []
for i in range(1,N+1):
list.append(int(A*i))
print(sum(list))
slqh(A,N)
else:
print('data error')
第16关 单词个数
txt = input()
for i in ",.!":
txt = txt.replace(i, " ")
x1 = txt.split()
print(len(x1))