CCF 201803-2 碰撞的小球 python 满分
题目叙述问题描述:略输入格式:略输出格式:略样例
满分证明解题思路满分代码满分代码优化
题目叙述
问题描述:略
输入格式:略
输出格式:略
样例
样例输入
3 10 5
4 6 8
样例输出
7 9 9
样例输入
10 22 30
14 12 16 6 10 2 8 20 18 4
样例输出
6 6 8 2 4 0 4 12 10 2
满分证明
解题思路
涉及3个重点: (1) 到左端点换方向; (2)到右端点换方向; (3) 两点相遇换方向;
最后,做一个列表加和函数。
满分代码
def
ass(a
, b
):
sum
= [0] * len(a
)
for i in
range(len(a
)):
sum
[i
] = a
[i
] + b
[i
]
return sum
n
, lc
, t
= map(int, input().split())
ll
= list(map(int, input().split()))
flag
= [1] * n
for _ in
range(t
):
if lc in ll
:
flag
[ll
.index(lc
)] = -flag
[ll
.index(lc
)]
if 0 in ll
:
flag
[ll
.index(0)] = -flag
[ll
.index(0)]
if len(ll
) != len(list(set(ll
))):
templ
= ll
.copy()
qcl
=sorted(list(set(ll
)))
for i in qcl
:
if ll
.count(i
) == 2:
flag
[templ
.index(i
)] = -flag
[templ
.index(i
)]
templ
[templ
.index(i
)] = -1
flag
[templ
.index(i
)] = -flag
[templ
.index(i
)]
ll
= ass(ll
, flag
)
for i in
range(n
):
print(ll
[i
], end
=" ")
满分代码优化
把函数优化到里面
n
, lc
, t
= map(int, input().split())
ll
= list(map(int, input().split()))
flag
= [1] * n
for _ in
range(t
):
if lc in ll
:
flag
[ll
.index(lc
)] = -flag
[ll
.index(lc
)]
if 0 in ll
:
flag
[ll
.index(0)] = -flag
[ll
.index(0)]
if len(ll
) != len(list(set(ll
))):
templ
= ll
.copy()
qcl
= sorted(list(set(ll
)))
for i in qcl
:
if ll
.count(i
) == 2:
flag
[templ
.index(i
)] = -flag
[templ
.index(i
)]
templ
[templ
.index(i
)] = -1
flag
[templ
.index(i
)] = -flag
[templ
.index(i
)]
ll
= [ll
[i
]+flag
[i
] for i in
range(len(ll
))]
for i in
range(n
):
print(ll
[i
], end
=" ")
转载请注明原文地址:https://blackberry.8miu.com/read-1656.html