用set判断是否输出过 + 筛质数
n = int(input())
mp = {}
def isprime(x):
if x < 2:
return False
for i in range(2,int(x ** 0.5) + 1):
if x % i == 0:
return False
return True
for rank in range(1,n + 1):
mp[input()] = rank
k = int(input())
flag = set()
while k > 0:
k -= 1
id = input()
if id in flag:
print(f"{id}: Checked")
continue
if id not in mp:
print(f"{id}: Are you kidding?")
else:
rank = mp[id]
if rank == 1:
res = "Mystery Award"
elif isprime(rank):
res = "Minion"
else:
res = "Chocolate"
print(f"{id}: {res}")
flag.add(id)
判断是否是质数那一步可以优化一下,可以提前把数据范围内的质数先筛出来,然后判断是否是质数直接查表即可:
N = (int)(1e4 + 10)
n = int(input())
mp = {}
st = [False] * N
primes = []
def get_primes(n): # 线性筛质数
for i in range(2,n + 1):
if not st[i]:
primes.append(i)
j = 0
while primes[j] * i <= n:
st[primes[j] * i] = True
if i % primes[j] == 0:
break
j += 1
get_primes(n)
for rank in range(1,n + 1):
mp[input()] = rank
k = int(input())
flag = set()
while k > 0:
k -= 1
id = input()
if id in flag:
print(f"{id}: Checked")
continue
if id not in mp:
print(f"{id}: Are you kidding?")
else:
rank = mp[id]
if rank == 1:
res = "Mystery Award"
elif not st[rank]:
res = "Minion"
else:
res = "Chocolate"
print(f"{id}: {res}")
flag.add(id)