CCF CSP Brush Question Record 24-201803-2 collision ball (java)

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.
  When the ball reaches the end point (left end or right end) of the line segment, it will immediately move in the opposite direction, and the speed remains the same.
  When the two small balls collide together, the two small balls will move in the opposite direction from the original moving direction, and continue to move at the original speed.
  Now, tell you the length L of the line segment, the number of balls n, and the initial positions of the n balls. Please calculate the position of each ball after t seconds.

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.
  At the same time, it can be proved that the position where two small balls collide must be an integer (but not necessarily an even number).

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.
  The second line contains n integers a1, a2, …, an, separated by spaces, indicating the positions of n balls at the initial moment.

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
4 6 8

Sample output

7 9 9

Sample description

  Initially, the positions of the three balls are 4, 6, 8 respectively.

  After one second, the positions of the three balls are 5, 7, 9 respectively.

  Two seconds later, the third ball hits the wall and the speed is reversed. The positions of the three balls are 6, 8, 10.

  Three seconds later, the second ball collides with the third ball at position 9, and the speed is reversed (note that the collision position is not necessarily an even number). The positions of the three balls are 7, 9, 9 respectively.

  Four seconds later, the first ball collides with the second ball at position 8, and the speed is reversed. The third ball hits the wall and the speed is reversed. The positions of the three balls are 8, 8, 10 respectively. .

  After five seconds, the positions of the three balls are 7, 9, 9 respectively.

Sample input

10 22 30
14 12 16 6 10 2 8 20 18 4

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.
  Ensure that the initial positions of all balls are different from each other and are even numbers.

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])+" ");
		}

	}

}

 

Guess you like

Origin blog.csdn.net/m0_37483148/article/details/108364486
Recommended