题目来源于CCF CSP
思路解析
根据碰撞的时候,会改变方向,那么我们将每一个小球的位置用数组存起来,遇到相同的位置且运动方向不同时,改变运动方向即可。
代码解析
#include<iostream>
using namespace std
;
struct point
{
int pos
;
int direction
;
}pos
[100];
int main()
{
int n
, L
, t
;
cin
>> n
>> L
>> t
;
for (int i
= 0; i
< n
; ++i
) {
cin
>> pos
[i
].pos
;
pos
[i
].direction
= 1;
}
for (int tt
= 0; tt
< t
; ++tt
) {
for (int i
= 0; i
< n
; ++i
) {
if (pos
[i
].direction
== 1) {
pos
[i
].pos
+= 1;
if (pos
[i
].pos
> L
) {
pos
[i
].pos
= L
- 1;
pos
[i
].direction
= -1;
}
}
else if (pos
[i
].direction
== -1) {
pos
[i
].pos
-= 1;
if (pos
[i
].pos
< 0) {
pos
[i
].pos
= 1;
pos
[i
].direction
= 1;
}
}
}
for (int i
= 0; i
< n
; ++i
) {
for (int j
= i
+ 1; j
< n
; ++j
) {
if (pos
[i
].pos
== pos
[j
].pos
) {
pos
[i
].direction
= -pos
[i
].direction
;
pos
[j
].direction
= -pos
[j
].direction
;
}
}
}
}
for (int i
= 0; i
< n
; ++i
)
cout
<< pos
[i
].pos
<< " ";
return 0;
}
转载请注明原文地址:https://tech.qufami.com/read-20839.html