PAT 1116 Come on! Let's C python解法

1116 Come on! Let’s C (20 分)
“Let’s C” is a popular and fun programming contest hosted by the College of Computer Science and Technology, Zhejiang University. Since the idea of the contest is for fun, the award rules are funny as the following:

0、 The Champion will receive a “Mystery Award” (such as a BIG collection of students’ research papers…).
1、 Those who ranked as a prime number will receive the best award – the Minions (小黄人)!
2、 Everyone else will receive chocolates.
Given the final ranklist and a sequence of contestant ID’s, you are supposed to tell the corresponding awards.

Input Specification:
Each input file contains one test case. For each case, the first line gives a positive integer N (≤10​4​), the total number of contestants. Then N lines of the ranklist follow, each in order gives a contestant’s ID (a 4-digit number). After the ranklist, there is a positive integer K followed by K query ID’s.

Output Specification:
For each query, print in a line ID: award where the award is Mystery Award, or Minion, or Chocolate. If the ID is not in the ranklist, print Are you kidding? instead. If the ID has been checked before, print ID: Checked.

Sample Input:
6
1111
6666
8888
1234
5555
0001
6
8888
0001
1111
2222
8888
2222
Sample Output:
8888: Minion
0001: Chocolate
1111: Mystery Award
2222: Are you kidding?
8888: Checked
2222: Are you kidding?

题意:给你一份排名,第一获得Mystery Award,排名是素数的人获得Minions,其他人获得 Chocolate。再给你一份名单,你需要输出每一个人获得的奖品,如果不在获奖名单中,输出Are you kidding?

解题思路一:扫描获奖名单,将他们分成3类,然后扫描给出的名单,判断属于哪一类,则输出相应的结果。注:两个测试点超时

def isprime(number):
    #number>1
    flag = False
    for i in range(2,number):
        if number%i == 0:
            flag = False
            break
    else:
        flag = True
    return flag

n = int(input())
Minion = {}
Chocolate = {}
Mystery_Award = input()
Mystery_Award_flag = 0

for i in range(2,n+1):
    if isprime(i):
        key = input()
        Minion[key] = 0
    else:
        key = input()
        Chocolate[key] = 0
#print('Minion',Minion)
#print('Chocolate',Chocolate)
m = int(input())
l = []
for i in range(m):
    l.append(input())

for i in l:
    if i == Mystery_Award:
        if Mystery_Award_flag == 0:
            print(i+':','Mystery Award')
            Mystery_Award_flag = 1
        else:
            print(i+':','Checked')
    elif i in Minion.keys():
        if Minion[i] == 0:
            print(i+':','Minion')
            Minion[i] = 1
        else:
            print(i+':','Checked')
    elif i in Chocolate.keys():
        if Chocolate[i] == 0:
            print(i+':','Chocolate')
            Chocolate[i] = 1
        else:
            print(i+':','Checked')  
    else:
        print(i+':','Are you kidding?')

解题思路二:建立一个长度能容纳所有人的列表rank,记录每个人对应的排名,用集合checked来存储查过的人,然后根据每个人的id判断应该输出什么。注:一个测试点超时

def isprime(number):
    #number>1
    flag = False
    for i in range(2,number):
        if number%i == 0:
            flag = False
            break
    else:
        flag = True
    return flag
rank = [0 for i in range(10001)]
#print(rank)
n = int(input())

for i in range(n):
    number = int(input())
    rank[number] = i + 1

m = int(input())
checked = set()
s = ''
for i in range(m):
    number = int(input())
    s +='%04d: '%number
    if(rank[number] == 0 ):
        s += 'Are you kidding?\n'
        continue
    if number in checked:
        s += 'Checked\n'
        continue
    else:
        checked.add(number)
    if rank[number] == 1:
        s += 'Mystery Award\n'
    elif isprime(rank[number]):
        s += 'Minion\n'
    else:
        s += 'Chocolate\n'

print(s,end='')

你可能感兴趣的:(python,用Python刷PAT,(Advanced,Level),Practice)