CCF Python题解(100分)201803-2 碰撞的小球

CCF Python题解(100分)201803-2 碰撞的小球

from collections import defaultdict

n, L, t = map(int,input().split())
positions = map(int,input().split())
list1 = [1 for i in range(n)]
for j in range(t):

    dict2 = defaultdict(list)
    for i, item in enumerate(positions):
        dict2[item].append(i)
    for key, l in dict2.items():
        if len(l) == 2:
            list1[l[0]] = -list1[l[0]]
            list1[l[1]] = -list1[l[1]]
    for index in range(n):
        if positions[index] == L or positions[index] == 0:
            list1[index] = -list1[index]
        positions[index] += list1[index]
for i in positions:
    print(i, end=' ')

你可能感兴趣的:(CCF历年题解(Python))