使用列表储存数据,需要创建序列号,要不然会超出索引,最后一个测试点超时
n = int(input())
score = list(map(int,input().split()))
m = list(map(int,input().split()))
k = m[0]
seek = m[1:]
result = []
for i in range(k):
result.append(0)
for j in range(n):
if seek[i] == score[j]:
result[i] += 1
for i in range(k):
if i == k-1:
print(result[i],end='')
else:
print(result[i],end=' ')
不使用列表,直接输出,也超时了
n = int(input())
score = list(map(int,input().split()))
m = list(map(int,input().split()))
k = m[0]
seek = m[1:]
for i in range(k):
count = 0
for j in range(n):
if seek[i] == score[j]:
count += 1
if i == k-1:
print(count,end='')
else:
print(count,end=' ')
使用循环遍历查找结果太慢,0-100共有101种结果,创建一个能储存101个值的列表,列表的序列号就是成绩,每读取一个成绩就将相应的序列号的值加1
不懂为什么最后一个测试点格式错误
n = int(input())
score = list(map(int,input().split()))
m = list(map(int,input().split()))
count_list = [0]*101
for i in score:
count_list[i] += 1
for i in m[1:]:
if i == m[-1]:
print(count_list[i])
else:
print(count_list[i],end=' ')
用这种格式输入就正确了
n = int(input())
score = list(map(int,input().split()))
m = list(map(int,input().split()))
count_list = [0]*101
for i in score:
count_list[i] += 1
for i in m[1:-1]:
print(count_list[i],end=' ')
print(count_list[m[-1]])