import java.util.Scanner;
public class Main2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int L = sc.nextInt();
int t = sc.nextInt();
int timeCount = 0;
Ball [] ball = new Ball[n];
for (int i = 0; i < n; i++)
ball[i] = new Ball(sc.nextInt(), 1);
while (++timeCount <= t) {
for (int i = 0; i < n; i++) {
if(ball[i].direction == 1) {
ball[i].position++;
if(ball[i].position == L)
ball[i].direction *= -1;
} else {
ball[i].position--;
if(ball[i].position == 0)
ball[i].direction *= -1;
}
}
for (int i = 0; i < n - 1; i++)
for (int j = i + 1; j < n; j++)
if (ball[i].position.equals(ball[j].position)) {
ball[i].direction *= -1;
ball[j].direction *= -1;
break;
}
}
for(int i = 0; i < n; i ++) {
System.out.print(ball[i].position + " ");
}
}
}
class Ball {
public Integer position;
public Integer direction;
public Ball(Integer position, Integer direction) {
this.position = position;
this.direction = direction;
}
}
另一种解法(小球之间的碰撞)
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
class Ball {
int position;
int direction;
public Ball(int position, int direction) {
this.position = position;
this.direction = direction;
}
}
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int L = scanner.nextInt();
int time = scanner.nextInt();
Ball[] balls = new Ball[n];
for(int i = 0; i < n;i++){
int pos = scanner.nextInt();
balls[i] = new Ball(pos, 1);
}
for(int t = 0; t < time; t++){
for(int i = 0; i < n; i++){
if(balls[i].direction == 1){
balls[i].position++;
if(balls[i].position == L){
balls[i].direction *= -1;
}
}
else{
balls[i].position--;
if(balls[i].position == 0){
balls[i].direction *= -1;
}
}
}
Map<Integer, Ball> map = new HashMap<>();
for(int i = 0; i < n; i++){
if(!map.containsKey(balls[i].position)){
map.put(balls[i].position, balls[i]);
}
else{
balls[i].direction *= -1;
map.get(balls[i].position).direction *= -1;
}
}
}
for(int i = 0; i < n; i++){
System.out.print(balls[i].position+ " ");
}
}
}