PAT2021-12-19乙级

test1.py

N,W = list(map(eval,input().split()))
# print(N,type(N),W,type(W))
weights = list(map(eval,input().split()))
# print(weights,type(weights))
melonNum, boxNum = 0, 0

currentWeight, currentMelon = 0, 0
for i in range(N):
    if currentWeight+weights[i] < W:
         currentWeight += (weights[i])
         currentMelon += 1
    elif currentWeight+weights[i] > W:
        pass
    else:  # 恰好相等
        melonNum += (1+currentMelon)
        boxNum += 1
        currentWeight, currentMelon = 0, 0
print(boxNum,melonNum)

test2.py

N = int(input())
# print(N,type(N))
info = []
med_stat = dict()
for i in range(N):
    item = input().split()
    info.append(item)
    for medicine in item[2:]:
        med_stat[medicine] = med_stat.get(medicine,0) + 1 
# print(info,type(info))

med_stat_list = list(med_stat.items())
med_stat_list.sort(key=lambda x:x[0])
med_stat_list.sort(key=lambda x:x[1],reverse=True)
print(med_stat_list[0][0],med_stat_list[0][1])

medicine = med_stat_list[0][0]
for item in info:
    if medicine in item:
        print(item[0])

test3-1.py

N = int(input())
# print(N,type(N))
colorsList = input().split()
colorsSet = set(colorsList)
colorsNum = len(colorsSet)
print(colorsNum)
result = list()
for color in colorsList:
    if color in result:
        pass
    else:
        result.append(color)

print(" ".join(result))

test3-2.py

N = int(input())
# print(N,type(N))
colorsList = input().split()
colorsSet = set(colorsList)
colorsNum = len(colorsSet)
print(colorsNum)
result = list()
for color in colorsList:
    if color not in colorsSet:
        pass
    else:
        result.append(color)
        colorsSet.remove(color)
        if len(colorsSet)==0:
            break

print(" ".join(result))

test4.py

N,M = list(map(eval,input().split()))  # 网站数量N, 事件数量M

suspect_cnt = dict()

for _ in range(M):
    cnt = [0]*N
    reports = input().split()
    min_cnt = N+1
    min_web = set()
    for i in range(N):
        cnt[i] = reports.count(reports[i])
        if cnt[i]<min_cnt:
            min_cnt=cnt[i]
            min_web.clear()
            min_web.add(i)
        elif cnt[i]==min_cnt:
            min_web.add(i)
    for i in min_web:
        suspect_cnt[i+1] = suspect_cnt.get(i+1,0) + 1

# print(suspect_cnt)
cnt_max = -1
web_max = -1
for web,cnt in suspect_cnt.items():
    if cnt>cnt_max:
        cnt_max = cnt
        web_max = web

print(web_max)

test4-2.py

N,M = list(map(eval,input().split()))  # 网站数量N, 事件数量M

suspect_cnt = dict()

for _ in range(M):
    reports = input().split()
    min_web = list()
    temp_dict = {}
    for ind, report in enumerate(reports):
        if report in temp_dict:
            (temp_dict[report]).append(ind)
        else:
            temp_dict[report] = [ind]
    temp_list = list(temp_dict.items())
    temp_list.sort(key=lambda x:len(x[1]))
    maxLen = len(temp_list[0][1])
    for report,webList in temp_list:
        if maxLen != len(webList):
            break
        else:
            min_web.extend(webList)

    for i in min_web:
        suspect_cnt[i+1] = suspect_cnt.get(i+1,0) + 1

# print(suspect_cnt)
cnt_max = -1
web_max = -1
for web,cnt in suspect_cnt.items():
    if cnt>cnt_max:
        cnt_max = cnt
        web_max = web

print(web_max)

test5.py

N = int(input())
# print(type(N),N)
info_list = list(map(eval,input().split()))
# print(info_list)
# 定义结点(prior,next)
Nodes = [[-1,-1] for i in range(N)]
for i in range(N):
    Nodes[i][1] = info_list[i]
    Nodes[info_list[i]][0] = i
# print(Nodes)
for head_inx in range(N):
    if Nodes[head_inx][0] == -1:
        break
result = [-1]*N
head = head_inx
lenth = 1
while head != -1:
    result[head] = str(lenth)
    lenth += 1
    head = Nodes[head][1]

print(" ".join(result))

test5-2.py

N = int(input())
# print(type(N),N)
info_list = list(map(eval,input().split()))
# print(info_list)
# 定义结点(prior,next)
# Nodes = [[-1,-1] for i in range(N)]
Nodes = dict()
for i in range(N):
    if i not in Nodes:
        Nodes[i] = [-1,-1]
    Nodes[i][1] = info_list[i]
    if info_list[i] not in Nodes:
        Nodes[info_list[i]] = [-1,-1]
    Nodes[info_list[i]][0] = i
# print(Nodes)
for head_inx in range(N):
    if Nodes[head_inx][0] == -1:
        break
result = [-1]*N
head = head_inx
lenth = 1
while head != -1:
    result[head] = str(lenth)
    lenth += 1
    head = Nodes[head][1]

print(" ".join(result))


版权声明:本文为m0_46653437原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。