Question number: | 201803-2 |
Question name: | Collision ball |
time limit: | 1.0s |
Memory limit: | 256.0MB |
Problem Description: | Problem Description There is a line segment of length L (L is an even number) on the number axis, the left end point is at the origin, and the right end point is at the coordinate L. There are n balls on the line segment regardless of volume. At the beginning, all the balls are on even-numbered coordinates. The speed direction is to the right, and the speed is 1 unit length per second. prompt Because the initial positions of all the balls are even numbers, and the length of the line segment is an even number, it can be proved that there will not be three balls colliding at the same time, and the time when the balls reach the end of the line segment and the collision between the balls are all integers. Input format The first line of input contains three integers n, L, t, separated by spaces, which represent the number of balls, the length of the line segment, and the position of the ball you need to calculate after t seconds. Output format The output line contains n integers, separated by spaces, and the i-th integer represents the ball at the initial moment ai, and the position after t seconds. Sample input 3 10 5 Sample output 7 9 9 Sample description Initially, the positions of the three balls are 4, 6, 8 respectively. Sample input 10 22 30 Sample output 6 6 8 2 4 0 4 12 10 2 Data size and convention For all evaluation cases, 1 ≤ n ≤ 100, 1 ≤ t ≤ 100, 2 ≤ L ≤ 1000, and 0 <ai <L. L is an even number. |
import java.util.Scanner;
public class 碰撞的小球 {
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[] a=new int[n];
for(int i=0;i<n;i++){
a[i]=sc.nextInt();
}
for(int t=0;t<T;t++){
for(int i=0;i<n;i++){
if(a[i]==L){
a[i]=(-1)*a[i];
}else if(a[i]==0){
a[i]=(-1)*a[i];
}
a[i]+=1;
}
for(int i=0;i<n;i++){
for(int j=i+1;j<n;j++){
if(Math.abs(a[i])==Math.abs(a[j])){
a[i]=(-1)*a[i];
a[j]=(-1)*a[j];
}
}
}
}
for(int i=0;i<n;i++){
System.out.print(Math.abs(a[i])+" ");
}
}
}