201803-2 Colliding balls

Problem Description
  There is a line segment on the number line of length L (L is an even number), the left endpoint is at the origin, and the right endpoint is at coordinate L. There are n small balls of no volume on the line segment. At the beginning, all the balls are on even-numbered coordinates, and the speed direction is rightward, 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 is still the original size.
  When the two balls collide, the two balls will move in the opposite direction to their original moving direction and continue to move at their 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.
hint
  Because the initial positions of all the balls are even, and the length of the line segment is even, it can be proved that there will be no three balls colliding at the same time, and the time when the ball reaches the end point of the line segment and the collision time between the balls is an integer.
  At the same time, it can also be proved that the position where the two 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, representing the positions of n balls at the initial moment.
output format
  The output line contains n integers, separated by spaces, the ith integer represents the position of the ball at ai at the initial moment, 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, and 8.

  One second later, the positions of the three balls are 5, 7, and 9.

  Two seconds later, the third ball hits the wall, the speed is reversed, and the positions of the three balls are 6, 8, and 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), and the positions of the three balls are 7, 9, and 9 respectively.

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

  Five seconds later, the positions of the three balls are 7, 9, and 9.
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 conventions
  For all evaluation cases, 1 ≤ n ≤ 100, 1 ≤ t ≤ 100, 2 ≤ L ≤ 1000, 0 < ai < L. L is an even number.

  Make sure that the initial positions of all the balls are different and even.

#include<stdio.h>
#define M 101
intmain()
{
    int n,l,t,t0,i,k,j,a[M][2];
    freopen("in.txt","r",stdin);
    scanf("%d%d%d",&n,&l,&t);
    for(i=0;i<n;i++)
    {
        a[i][0]=1;
    }
    if(l%2==0)
    {
        for(i=0;i<n;i++)
        {
            scanf("%d",&a[i][1]);
        }
        for(t0=0;t0<t;t0++)
        {
            for(i=0;i<n;i++)
            {
                if(a[i][1]==l)
                {
                    a[i][0]=-1;
                }
                else if(a[i]==0)
                {
                    a[i][0]=1;
                }
                for(j=i+1;j<n;j++)
                {
                    if(a[i][1]==a[j][1])
                    {
                        a[i][0]=-a[i][0];
                        a[j][0]=-a[i][0];
                    }
                }
                a[i][1]=a[i][1]+a[i][0];
            }
            for(k=0;k<n;k++)
        {
            if(k%9==0&&k!=0)
            {
                printf("%d\n",a[k][1]);
            }
            else
                printf("%d ",a[k][1]);
        }
        }
        for(i=0;i<n;i++)
        {
            printf("%d ",a[i][1]);
        }
    }
    return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324419797&siteId=291194637